@@ -17,117 +17,117 @@ discard block |
||
| 17 | 17 | $client = new \EasyHttp\Client(); |
| 18 | 18 | $endpoint = 'https://scanner.tradingview.com/america/scan'; |
| 19 | 19 | $post = [ |
| 20 | - 'filter' => [ |
|
| 21 | - [ |
|
| 22 | - 'left' => "exchange", |
|
| 23 | - 'operation' => "in_range", |
|
| 24 | - 'right' => [ |
|
| 25 | - 'AMEX', |
|
| 26 | - 'NASDAQ', |
|
| 27 | - 'NYSE' |
|
| 28 | - ], |
|
| 29 | - ], |
|
| 30 | - [ |
|
| 31 | - 'left' => "is_primary", |
|
| 32 | - 'operation' => "equal", |
|
| 33 | - 'right' => true, |
|
| 34 | - ], |
|
| 35 | - [ |
|
| 36 | - 'left' => "change", |
|
| 37 | - 'operation' => "nempty", |
|
| 38 | - ] |
|
| 39 | - ], |
|
| 40 | - 'options' => [ |
|
| 41 | - 'lang' => "en", |
|
| 42 | - 'active_symbols_only' => true, |
|
| 43 | - ], |
|
| 44 | - 'markets' => [ |
|
| 45 | - "america" |
|
| 46 | - ], |
|
| 47 | - 'columns' => [ |
|
| 48 | - "logoid", |
|
| 49 | - "name", |
|
| 50 | - "change|1", |
|
| 51 | - "change|5", |
|
| 52 | - "change|15", |
|
| 53 | - "change|60", |
|
| 54 | - "change|240", |
|
| 55 | - "change", |
|
| 56 | - "change|1W", |
|
| 57 | - "change|1M", |
|
| 58 | - "Perf.3M", |
|
| 59 | - "Perf.6M", |
|
| 60 | - "Perf.YTD", |
|
| 61 | - "Perf.Y", |
|
| 62 | - "Volatility.D", |
|
| 63 | - "description", |
|
| 64 | - "type", |
|
| 65 | - "subtype", |
|
| 66 | - "update_mode", |
|
| 67 | - "currency", |
|
| 68 | - "fundamental_currency_code", |
|
| 69 | - "Recommend.All", |
|
| 70 | - "RSI", |
|
| 71 | - "RSI[1]", |
|
| 72 | - "Stoch.K", |
|
| 73 | - "Stoch.D", |
|
| 74 | - "Stoch.K[1]", |
|
| 75 | - "Stoch.D[1]", |
|
| 76 | - "CCI20", |
|
| 77 | - "CCI20[1]", |
|
| 78 | - "AO", |
|
| 79 | - "AO[1]", |
|
| 80 | - "AO[2]", |
|
| 81 | - "Mom", |
|
| 82 | - "Mom[1]", |
|
| 83 | - "MACD.macd", |
|
| 84 | - "MACD.signal", |
|
| 85 | - "Rec.Stoch.RSI", |
|
| 86 | - "Stoch.RSI.K", |
|
| 87 | - "Rec.WR", |
|
| 88 | - "W.R", |
|
| 89 | - "Rec.BBPower", |
|
| 90 | - "BBPower", |
|
| 91 | - "Rec.UO", |
|
| 92 | - "UO", |
|
| 93 | - "EMA10", |
|
| 94 | - "close", |
|
| 95 | - "SMA10", |
|
| 96 | - "EMA20", |
|
| 97 | - "SMA20", |
|
| 98 | - "EMA30", |
|
| 99 | - "SMA30", |
|
| 100 | - "EMA50", |
|
| 101 | - "SMA50", |
|
| 102 | - "EMA100", |
|
| 103 | - "SMA100", |
|
| 104 | - "EMA200", |
|
| 105 | - "SMA200", |
|
| 106 | - "Rec.Ichimoku", |
|
| 107 | - "Ichimoku.BLine", |
|
| 108 | - "Rec.VWMA", |
|
| 109 | - "VWMA", |
|
| 110 | - "Rec.HullMA9", |
|
| 111 | - "HullMA9" |
|
| 112 | - ], |
|
| 113 | - 'sort' => [ |
|
| 114 | - 'sortBy' => "change", |
|
| 115 | - 'sortOrder' => "desc" |
|
| 116 | - ] |
|
| 20 | + 'filter' => [ |
|
| 21 | + [ |
|
| 22 | + 'left' => "exchange", |
|
| 23 | + 'operation' => "in_range", |
|
| 24 | + 'right' => [ |
|
| 25 | + 'AMEX', |
|
| 26 | + 'NASDAQ', |
|
| 27 | + 'NYSE' |
|
| 28 | + ], |
|
| 29 | + ], |
|
| 30 | + [ |
|
| 31 | + 'left' => "is_primary", |
|
| 32 | + 'operation' => "equal", |
|
| 33 | + 'right' => true, |
|
| 34 | + ], |
|
| 35 | + [ |
|
| 36 | + 'left' => "change", |
|
| 37 | + 'operation' => "nempty", |
|
| 38 | + ] |
|
| 39 | + ], |
|
| 40 | + 'options' => [ |
|
| 41 | + 'lang' => "en", |
|
| 42 | + 'active_symbols_only' => true, |
|
| 43 | + ], |
|
| 44 | + 'markets' => [ |
|
| 45 | + "america" |
|
| 46 | + ], |
|
| 47 | + 'columns' => [ |
|
| 48 | + "logoid", |
|
| 49 | + "name", |
|
| 50 | + "change|1", |
|
| 51 | + "change|5", |
|
| 52 | + "change|15", |
|
| 53 | + "change|60", |
|
| 54 | + "change|240", |
|
| 55 | + "change", |
|
| 56 | + "change|1W", |
|
| 57 | + "change|1M", |
|
| 58 | + "Perf.3M", |
|
| 59 | + "Perf.6M", |
|
| 60 | + "Perf.YTD", |
|
| 61 | + "Perf.Y", |
|
| 62 | + "Volatility.D", |
|
| 63 | + "description", |
|
| 64 | + "type", |
|
| 65 | + "subtype", |
|
| 66 | + "update_mode", |
|
| 67 | + "currency", |
|
| 68 | + "fundamental_currency_code", |
|
| 69 | + "Recommend.All", |
|
| 70 | + "RSI", |
|
| 71 | + "RSI[1]", |
|
| 72 | + "Stoch.K", |
|
| 73 | + "Stoch.D", |
|
| 74 | + "Stoch.K[1]", |
|
| 75 | + "Stoch.D[1]", |
|
| 76 | + "CCI20", |
|
| 77 | + "CCI20[1]", |
|
| 78 | + "AO", |
|
| 79 | + "AO[1]", |
|
| 80 | + "AO[2]", |
|
| 81 | + "Mom", |
|
| 82 | + "Mom[1]", |
|
| 83 | + "MACD.macd", |
|
| 84 | + "MACD.signal", |
|
| 85 | + "Rec.Stoch.RSI", |
|
| 86 | + "Stoch.RSI.K", |
|
| 87 | + "Rec.WR", |
|
| 88 | + "W.R", |
|
| 89 | + "Rec.BBPower", |
|
| 90 | + "BBPower", |
|
| 91 | + "Rec.UO", |
|
| 92 | + "UO", |
|
| 93 | + "EMA10", |
|
| 94 | + "close", |
|
| 95 | + "SMA10", |
|
| 96 | + "EMA20", |
|
| 97 | + "SMA20", |
|
| 98 | + "EMA30", |
|
| 99 | + "SMA30", |
|
| 100 | + "EMA50", |
|
| 101 | + "SMA50", |
|
| 102 | + "EMA100", |
|
| 103 | + "SMA100", |
|
| 104 | + "EMA200", |
|
| 105 | + "SMA200", |
|
| 106 | + "Rec.Ichimoku", |
|
| 107 | + "Ichimoku.BLine", |
|
| 108 | + "Rec.VWMA", |
|
| 109 | + "VWMA", |
|
| 110 | + "Rec.HullMA9", |
|
| 111 | + "HullMA9" |
|
| 112 | + ], |
|
| 113 | + 'sort' => [ |
|
| 114 | + 'sortBy' => "change", |
|
| 115 | + 'sortOrder' => "desc" |
|
| 116 | + ] |
|
| 117 | 117 | ]; |
| 118 | 118 | |
| 119 | 119 | $start = microtime(true); |
| 120 | 120 | $Response = $client->post($endpoint, [ |
| 121 | - 'headers' => [ |
|
| 122 | - 'Content-Type' => 'application/json', |
|
| 123 | - 'Accept' => 'application/json', |
|
| 124 | - ], |
|
| 125 | - 'body' => array_merge($post, [ |
|
| 126 | - 'range' => [ |
|
| 127 | - 0, |
|
| 128 | - 2000 |
|
| 129 | - ] |
|
| 130 | - ]), |
|
| 121 | + 'headers' => [ |
|
| 122 | + 'Content-Type' => 'application/json', |
|
| 123 | + 'Accept' => 'application/json', |
|
| 124 | + ], |
|
| 125 | + 'body' => array_merge($post, [ |
|
| 126 | + 'range' => [ |
|
| 127 | + 0, |
|
| 128 | + 2000 |
|
| 129 | + ] |
|
| 130 | + ]), |
|
| 131 | 131 | ]); |
| 132 | 132 | $Data = json_decode($Response->getBody(), true); |
| 133 | 133 | $Memory = \EasyHttp\Utils\Toolkit::bytesToHuman(memory_get_usage()); |
@@ -139,22 +139,22 @@ discard block |
||
| 139 | 139 | // Initializes the request |
| 140 | 140 | $requests = []; |
| 141 | 141 | for ($i = 0; $i < 2000; $i += 500) { |
| 142 | - $requests[] = [ |
|
| 143 | - 'method' => 'POST', |
|
| 144 | - 'uri' => $endpoint, |
|
| 145 | - 'options' => [ |
|
| 146 | - 'headers' => [ |
|
| 147 | - 'Content-Type' => 'application/json', |
|
| 148 | - 'Accept' => 'application/json', |
|
| 149 | - ], |
|
| 150 | - 'body' => array_merge($post, [ |
|
| 151 | - 'range' => [ |
|
| 152 | - $i, |
|
| 153 | - $i + 500 |
|
| 154 | - ] |
|
| 155 | - ]), |
|
| 156 | - ] |
|
| 157 | - ]; |
|
| 142 | + $requests[] = [ |
|
| 143 | + 'method' => 'POST', |
|
| 144 | + 'uri' => $endpoint, |
|
| 145 | + 'options' => [ |
|
| 146 | + 'headers' => [ |
|
| 147 | + 'Content-Type' => 'application/json', |
|
| 148 | + 'Accept' => 'application/json', |
|
| 149 | + ], |
|
| 150 | + 'body' => array_merge($post, [ |
|
| 151 | + 'range' => [ |
|
| 152 | + $i, |
|
| 153 | + $i + 500 |
|
| 154 | + ] |
|
| 155 | + ]), |
|
| 156 | + ] |
|
| 157 | + ]; |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | // --------------------- ====== --------------------- // |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | $start = microtime(true); |
| 163 | 163 | $responses = $client->bulk($requests); |
| 164 | 164 | foreach ($responses as $response) { |
| 165 | - $Data = json_decode($response->getBody(), true); |
|
| 165 | + $Data = json_decode($response->getBody(), true); |
|
| 166 | 166 | } |
| 167 | 167 | $Memory = \EasyHttp\Utils\Toolkit::bytesToHuman(memory_get_usage()); |
| 168 | 168 | echo '<pre>' . "Bulk Request - Total time: " . (microtime(true) - $start) . " - Memory: $Memory" . '</pre>'; |
| 169 | 169 | \ No newline at end of file |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | |
| 14 | 14 | |
| 15 | 15 | if ($Result->save(__DIR__ . '/uploads/google.png')) { |
| 16 | - echo 'File saved successfully'; |
|
| 16 | + echo 'File saved successfully'; |
|
| 17 | 17 | } else { |
| 18 | - echo 'File not saved'; |
|
| 18 | + echo 'File not saved'; |
|
| 19 | 19 | } |
| 20 | 20 | \ No newline at end of file |
@@ -2,15 +2,15 @@ |
||
| 2 | 2 | |
| 3 | 3 | function generateUpToDateMimeArray(string $url): array |
| 4 | 4 | { |
| 5 | - $s = array(); |
|
| 6 | - foreach (@explode("\n", @file_get_contents($url)) as $x) { |
|
| 7 | - if (isset($x[0]) && $x[0] !== '#' && preg_match_all('#([^\s]+)#', $x, $out) && isset($out[1]) && ($c = count($out[1])) > 1) { |
|
| 8 | - for ($i = 1; $i < $c; $i++) { |
|
| 9 | - $s[$out[1][$i]] = $out[1][0]; |
|
| 10 | - } |
|
| 11 | - } |
|
| 12 | - } |
|
| 13 | - return $s; |
|
| 5 | + $s = array(); |
|
| 6 | + foreach (@explode("\n", @file_get_contents($url)) as $x) { |
|
| 7 | + if (isset($x[0]) && $x[0] !== '#' && preg_match_all('#([^\s]+)#', $x, $out) && isset($out[1]) && ($c = count($out[1])) > 1) { |
|
| 8 | + for ($i = 1; $i < $c; $i++) { |
|
| 9 | + $s[$out[1][$i]] = $out[1][0]; |
|
| 10 | + } |
|
| 11 | + } |
|
| 12 | + } |
|
| 13 | + return $s; |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | $data = generateUpToDateMimeArray('https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'); |
@@ -8,13 +8,13 @@ |
||
| 8 | 8 | $chat_id = '259760855'; |
| 9 | 9 | |
| 10 | 10 | $response = (new \EasyHttp\Client())->post('https://api.telegram.org/bot' . $your_token . '/sendPhoto', [ |
| 11 | - 'query' => [ |
|
| 12 | - 'chat_id' => $chat_id, |
|
| 13 | - 'caption' => 'Привет!', |
|
| 14 | - ], |
|
| 15 | - 'multipart' => \EasyHttp\FormData::create([ |
|
| 16 | - 'photo' => getcwd() . '/../docs/uploads/download.png' |
|
| 17 | - ]) |
|
| 11 | + 'query' => [ |
|
| 12 | + 'chat_id' => $chat_id, |
|
| 13 | + 'caption' => 'Привет!', |
|
| 14 | + ], |
|
| 15 | + 'multipart' => \EasyHttp\FormData::create([ |
|
| 16 | + 'photo' => getcwd() . '/../docs/uploads/download.png' |
|
| 17 | + ]) |
|
| 18 | 18 | ]); |
| 19 | 19 | |
| 20 | 20 | |
@@ -5,8 +5,8 @@ |
||
| 5 | 5 | // =============================== ***** =============================== // |
| 6 | 6 | |
| 7 | 7 | $result = (new \EasyHttp\Client())->upload('https://httpbin.org/post', [ |
| 8 | - 'photo1' => getcwd() . '/../docs/uploads/result-of-breakdown-example.png', |
|
| 9 | - 'photo2' => getcwd() . '/../docs/uploads/download.png', |
|
| 8 | + 'photo1' => getcwd() . '/../docs/uploads/result-of-breakdown-example.png', |
|
| 9 | + 'photo2' => getcwd() . '/../docs/uploads/download.png', |
|
| 10 | 10 | ]); |
| 11 | 11 | |
| 12 | 12 | echo '<pre>' . \EasyHttp\Utils\Toolkit::prettyJson($result->getBody()) . '</pre>'; |
@@ -13,41 +13,41 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | function startTicker(array $ids, callable $callback): void |
| 15 | 15 | { |
| 16 | - try { |
|
| 17 | - |
|
| 18 | - $close_time = time() + 10; |
|
| 19 | - $ClientConfig = (new WSConfig())->setFragmentSize(8096)->setTimeout(15); |
|
| 20 | - $WebSocketClient = new WebSocket('wss://stream.coinmarketcap.com/price/latest', $ClientConfig); |
|
| 21 | - |
|
| 22 | - $WebSocketClient->send(json_encode([ |
|
| 23 | - 'method' => 'subscribe', |
|
| 24 | - 'id' => 'price', |
|
| 25 | - 'data' => [ |
|
| 26 | - 'cryptoIds' => $ids, |
|
| 27 | - 'index' => 'detail' |
|
| 28 | - ] |
|
| 29 | - ])); |
|
| 30 | - |
|
| 31 | - while ($close_time > time()) { |
|
| 32 | - if (($message = $WebSocketClient->receive()) != "") { |
|
| 33 | - $json_response = json_decode($message, true); |
|
| 34 | - |
|
| 35 | - if ($json_response['id'] == "price") { |
|
| 36 | - $callback($json_response); |
|
| 37 | - } |
|
| 38 | - } |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - } catch (Exception $e) { |
|
| 42 | - echo "<b>Error</b>: " . $e->getMessage(); |
|
| 43 | - } |
|
| 16 | + try { |
|
| 17 | + |
|
| 18 | + $close_time = time() + 10; |
|
| 19 | + $ClientConfig = (new WSConfig())->setFragmentSize(8096)->setTimeout(15); |
|
| 20 | + $WebSocketClient = new WebSocket('wss://stream.coinmarketcap.com/price/latest', $ClientConfig); |
|
| 21 | + |
|
| 22 | + $WebSocketClient->send(json_encode([ |
|
| 23 | + 'method' => 'subscribe', |
|
| 24 | + 'id' => 'price', |
|
| 25 | + 'data' => [ |
|
| 26 | + 'cryptoIds' => $ids, |
|
| 27 | + 'index' => 'detail' |
|
| 28 | + ] |
|
| 29 | + ])); |
|
| 30 | + |
|
| 31 | + while ($close_time > time()) { |
|
| 32 | + if (($message = $WebSocketClient->receive()) != "") { |
|
| 33 | + $json_response = json_decode($message, true); |
|
| 34 | + |
|
| 35 | + if ($json_response['id'] == "price") { |
|
| 36 | + $callback($json_response); |
|
| 37 | + } |
|
| 38 | + } |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + } catch (Exception $e) { |
|
| 42 | + echo "<b>Error</b>: " . $e->getMessage(); |
|
| 43 | + } |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | echo "<pre>Start Tome: " . date('Y-m-d H:i:s') . "</pre><br/>"; |
| 47 | 47 | |
| 48 | 48 | $responses = []; |
| 49 | 49 | startTicker([1, 1027, 825, 3408, 1839, 4687, 52, 2010, 5426], function ($data) use (&$responses) { |
| 50 | - $responses[] = $data; |
|
| 50 | + $responses[] = $data; |
|
| 51 | 51 | }); |
| 52 | 52 | |
| 53 | 53 | echo "<pre>" . json_encode($responses, JSON_PRETTY_PRINT) . "</pre><br/>"; |
@@ -46,7 +46,7 @@ |
||
| 46 | 46 | echo "<pre>Start Tome: " . date('Y-m-d H:i:s') . "</pre><br/>"; |
| 47 | 47 | |
| 48 | 48 | $responses = []; |
| 49 | -startTicker([1, 1027, 825, 3408, 1839, 4687, 52, 2010, 5426], function ($data) use (&$responses) { |
|
| 49 | +startTicker([1, 1027, 825, 3408, 1839, 4687, 52, 2010, 5426], function($data) use (&$responses) { |
|
| 50 | 50 | $responses[] = $data; |
| 51 | 51 | }); |
| 52 | 52 | |
@@ -21,332 +21,332 @@ |
||
| 21 | 21 | class Client |
| 22 | 22 | { |
| 23 | 23 | |
| 24 | - use ClientTrait; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * The temp directory to download files - default is $_SERVER['TEMP'] |
|
| 28 | - * |
|
| 29 | - * @var ?string |
|
| 30 | - */ |
|
| 31 | - private ?string $tempDir; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * The Max count of chunk to download file |
|
| 35 | - * |
|
| 36 | - * @var int |
|
| 37 | - */ |
|
| 38 | - public int $maxChunkCount = 10; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The constructor of the client |
|
| 42 | - */ |
|
| 43 | - public function __construct() |
|
| 44 | - { |
|
| 45 | - $this->tempDir = $_SERVER['TEMP'] ?? null; |
|
| 46 | - $this->setHasSelfSignedCertificate(true); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Set has self-signed certificate |
|
| 51 | - * |
|
| 52 | - * This is used to set the curl option CURLOPT_SSL_VERIFYPEER |
|
| 53 | - * and CURLOPT_SSL_VERIFYHOST to false. This is useful when you are |
|
| 54 | - * in local environment, or you have self-signed certificate. |
|
| 55 | - * |
|
| 56 | - * @param bool $has |
|
| 57 | - * |
|
| 58 | - * @return void |
|
| 59 | - */ |
|
| 60 | - public function setHasSelfSignedCertificate(bool $has): void |
|
| 61 | - { |
|
| 62 | - putenv('HAS_SELF_SIGNED_CERT=' . ($has ? 'true' : 'false')); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Set the temporary directory path to save the downloaded files |
|
| 67 | - * |
|
| 68 | - * @param string $path |
|
| 69 | - * |
|
| 70 | - * @return void |
|
| 71 | - */ |
|
| 72 | - public function setTempPath(string $path): void |
|
| 73 | - { |
|
| 74 | - if (!file_exists($path)) { |
|
| 75 | - throw new InvalidArgumentException( |
|
| 76 | - sprintf('The path "%s" does not exist', $path) |
|
| 77 | - ); |
|
| 78 | - } |
|
| 79 | - $this->tempDir = $path; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * This method is used to send a http request to a given url. |
|
| 84 | - * |
|
| 85 | - * @param string $method |
|
| 86 | - * @param string $uri |
|
| 87 | - * @param array|HttpOptions $options |
|
| 88 | - * |
|
| 89 | - * @return HttpResponse |
|
| 90 | - */ |
|
| 91 | - public function request(string $method, string $uri, array|HttpOptions $options = []): HttpResponse |
|
| 92 | - { |
|
| 93 | - $CurlHandle = Middleware::create_curl_handler($method, $uri, $options); |
|
| 94 | - if (!$CurlHandle) { |
|
| 95 | - throw new RuntimeException('An error occurred while creating the curl handler'); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - $result = new HttpResponse(); |
|
| 99 | - $result->setCurlHandle($CurlHandle); |
|
| 100 | - |
|
| 101 | - $response = curl_exec($CurlHandle); |
|
| 102 | - if (curl_errno($CurlHandle) || !$response) { |
|
| 103 | - $result->setErrorCode(curl_errno($CurlHandle)); |
|
| 104 | - $result->setErrorMessage(curl_error($CurlHandle) ?? ErrorCode::getMessage(curl_errno($CurlHandle))); |
|
| 105 | - return $result; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $result->setStatusCode(curl_getinfo($CurlHandle, CURLINFO_HTTP_CODE)); |
|
| 109 | - $result->setHeaderSize(curl_getinfo($CurlHandle, CURLINFO_HEADER_SIZE)); |
|
| 110 | - $result->setHeaders(substr((string)$response, 0, $result->getHeaderSize())); |
|
| 111 | - $result->setBody(substr((string)$response, $result->getHeaderSize())); |
|
| 112 | - |
|
| 113 | - curl_close($CurlHandle); |
|
| 114 | - |
|
| 115 | - return $result; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Send multiple requests to a given url. |
|
| 120 | - * |
|
| 121 | - * @param array $requests [{method, uri, options}, ...] |
|
| 122 | - * |
|
| 123 | - * @return array<HttpResponse> |
|
| 124 | - */ |
|
| 125 | - public function bulk(array $requests): array |
|
| 126 | - { |
|
| 127 | - $result = []; |
|
| 128 | - $handlers = []; |
|
| 129 | - $multi_handler = curl_multi_init(); |
|
| 130 | - foreach ($requests as $request) { |
|
| 131 | - |
|
| 132 | - $CurlHandle = Middleware::create_curl_handler( |
|
| 133 | - $request['method'] ?? null, |
|
| 134 | - $request['uri'], |
|
| 135 | - $request['options'] ?? [] |
|
| 136 | - ); |
|
| 137 | - if (!$CurlHandle) { |
|
| 138 | - throw new RuntimeException( |
|
| 139 | - 'An error occurred while creating the curl handler' |
|
| 140 | - ); |
|
| 141 | - } |
|
| 142 | - $handlers[] = $CurlHandle; |
|
| 143 | - curl_multi_add_handle($multi_handler, $CurlHandle); |
|
| 144 | - |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - $active = null; |
|
| 148 | - do { |
|
| 149 | - $mrc = curl_multi_exec($multi_handler, $active); |
|
| 150 | - } while ($mrc == CURLM_CALL_MULTI_PERFORM); |
|
| 151 | - |
|
| 152 | - while ($active && $mrc == CURLM_OK) { |
|
| 153 | - if (curl_multi_select($multi_handler) != -1) { |
|
| 154 | - do { |
|
| 155 | - $mrc = curl_multi_exec($multi_handler, $active); |
|
| 156 | - } while ($mrc == CURLM_CALL_MULTI_PERFORM); |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - foreach ($handlers as $handler) { |
|
| 161 | - curl_multi_remove_handle($multi_handler, $handler); |
|
| 162 | - } |
|
| 163 | - curl_multi_close($multi_handler); |
|
| 164 | - |
|
| 165 | - foreach ($handlers as $handler) { |
|
| 166 | - $content = curl_multi_getcontent($handler); |
|
| 167 | - $response = new HttpResponse(); |
|
| 168 | - |
|
| 169 | - if (curl_errno($handler)) { |
|
| 170 | - $response->setErrorCode(curl_errno($handler)); |
|
| 171 | - $response->setErrorMessage( |
|
| 172 | - curl_error($handler) ?? |
|
| 173 | - ErrorCode::getMessage(curl_errno($handler)) |
|
| 174 | - ); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $response->setCurlHandle($handler); |
|
| 178 | - $response->setStatusCode(curl_getinfo($handler, CURLINFO_HTTP_CODE)); |
|
| 179 | - $response->setHeaderSize(curl_getinfo($handler, CURLINFO_HEADER_SIZE)); |
|
| 180 | - $response->setHeaders(substr($content, 0, $response->getHeaderSize())); |
|
| 181 | - $response->setBody(substr($content, $response->getHeaderSize())); |
|
| 182 | - |
|
| 183 | - $result[] = $response; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - return $result; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Download large files. |
|
| 191 | - * |
|
| 192 | - * This method is used to download large files with creating multiple requests. |
|
| 193 | - * |
|
| 194 | - * Change `max_chunk_count` variable to change the number of chunks. (default: 10) |
|
| 195 | - * |
|
| 196 | - * @param string $url The direct url to the file. |
|
| 197 | - * @param array|HttpOptions $options The options to use. |
|
| 198 | - * |
|
| 199 | - * @return DownloadResult |
|
| 200 | - */ |
|
| 201 | - public function download(string $url, array|HttpOptions $options = []): DownloadResult |
|
| 202 | - { |
|
| 203 | - if (empty($this->tempDir)) { |
|
| 204 | - throw new RuntimeException( |
|
| 205 | - 'The temp directory is not set. Please set the temp directory using the `setTempDir` method.' |
|
| 206 | - ); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - if (!file_exists($this->tempDir)) { |
|
| 210 | - if (mkdir($this->tempDir, 0777, true) === false) { |
|
| 211 | - throw new RuntimeException( |
|
| 212 | - 'The temp directory is not writable. Please set the temp directory using the `setTempDir` method.' |
|
| 213 | - ); |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if (gettype($options) === 'array') { |
|
| 218 | - $options = new HttpOptions($options); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $fileSize = $this->get_file_size($url); |
|
| 222 | - $chunkSize = $this->get_chunk_size($fileSize); |
|
| 223 | - |
|
| 224 | - $result = new DownloadResult(); |
|
| 225 | - |
|
| 226 | - $result->id = uniqid(); |
|
| 227 | - $result->chunksPath = $this->tempDir . '/' . $result->id . '/'; |
|
| 228 | - mkdir($result->chunksPath, 0777, true); |
|
| 229 | - |
|
| 230 | - $result->fileSize = $fileSize; |
|
| 231 | - $result->chunkSize = $chunkSize; |
|
| 232 | - $result->chunks = ceil($fileSize / $chunkSize); |
|
| 233 | - |
|
| 234 | - $result->startTime = time(); |
|
| 235 | - |
|
| 236 | - $requests = []; |
|
| 237 | - for ($i = 0; $i < $result->chunks; $i++) { |
|
| 238 | - $range = $i * $chunkSize . '-' . ($i + 1) * $chunkSize; |
|
| 239 | - if ($i + 1 === $result->chunks) { |
|
| 240 | - $range = $i * $chunkSize . '-' . $fileSize; |
|
| 241 | - } |
|
| 242 | - $requests[] = [ |
|
| 243 | - 'method' => 'GET', |
|
| 244 | - 'uri' => $url, |
|
| 245 | - 'options' => array_merge($options->toArray(), [ |
|
| 246 | - 'CurlOptions' => [ |
|
| 247 | - CURLOPT_RANGE => $range |
|
| 248 | - ], |
|
| 249 | - ]) |
|
| 250 | - ]; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - foreach ($this->bulk($requests) as $response) { |
|
| 254 | - $result->addChunk( |
|
| 255 | - Toolkit::randomString(16), |
|
| 256 | - $response->getBody(), |
|
| 257 | - $response->getInfoFromCurl()->TOTAL_TIME |
|
| 258 | - ); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - $result->endTime = time(); |
|
| 262 | - |
|
| 263 | - return $result; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Upload single or multiple files |
|
| 268 | - * |
|
| 269 | - * This method is sending file with request method of POST and |
|
| 270 | - * Content-Type of multipart/form-data. |
|
| 271 | - * |
|
| 272 | - * @param string $url The direct url to the file. |
|
| 273 | - * @param array $filePath The path to the file. |
|
| 274 | - * @param array|HttpOptions $options The options to use. |
|
| 275 | - * |
|
| 276 | - * @return HttpResponse |
|
| 277 | - */ |
|
| 278 | - public function upload(string $url, array $filePath, array|HttpOptions $options = []): HttpResponse |
|
| 279 | - { |
|
| 280 | - if (gettype($options) === 'array') { |
|
| 281 | - $options = new HttpOptions($options); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - $multipart = []; |
|
| 285 | - |
|
| 286 | - foreach ($filePath as $key => $file) { |
|
| 287 | - $multipart[$key] = new \CURLFile( |
|
| 288 | - realpath($file), |
|
| 289 | - $this->get_file_type($file) |
|
| 290 | - ); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - $options->setMultipart($multipart); |
|
| 294 | - return $this->post($url, $options); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * Get filetype with the extension. |
|
| 299 | - * |
|
| 300 | - * @param string $filename The absolute path to the file. |
|
| 301 | - * @return string eg. image/jpeg |
|
| 302 | - */ |
|
| 303 | - public static function get_file_type(string $filename): string |
|
| 304 | - { |
|
| 305 | - return MimeType::TYPES[pathinfo($filename, PATHINFO_EXTENSION)] ?? 'application/octet-stream'; |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Get file size. |
|
| 310 | - * |
|
| 311 | - * @param string $url The direct url to the file. |
|
| 312 | - * @return int |
|
| 313 | - */ |
|
| 314 | - public function get_file_size(string $url): int |
|
| 315 | - { |
|
| 316 | - if (file_exists($url)) { |
|
| 317 | - return filesize($url); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - $response = $this->get($url, [ |
|
| 321 | - 'headers' => [ |
|
| 322 | - 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36', |
|
| 323 | - ], |
|
| 324 | - 'CurlOptions' => [ |
|
| 325 | - CURLOPT_NOBODY => true, |
|
| 326 | - ] |
|
| 327 | - ]); |
|
| 328 | - |
|
| 329 | - return (int)$response->getHeaderLine('Content-Length') ?? 0; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Get the size of each chunk. |
|
| 334 | - * |
|
| 335 | - * For default, we're dividing filesize to 10 as max size of each chunk. |
|
| 336 | - * If the file size was smaller than 2MB, we'll use the filesize as single chunk. |
|
| 337 | - * |
|
| 338 | - * @param int $fileSize The file size. |
|
| 339 | - * @return int |
|
| 340 | - */ |
|
| 341 | - private function get_chunk_size(int $fileSize): int |
|
| 342 | - { |
|
| 343 | - $maxChunkSize = $fileSize / $this->maxChunkCount; |
|
| 344 | - |
|
| 345 | - if ($fileSize <= 2 * 1024 * 1024) { |
|
| 346 | - return $fileSize; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - return min($maxChunkSize, $fileSize); |
|
| 350 | - } |
|
| 24 | + use ClientTrait; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * The temp directory to download files - default is $_SERVER['TEMP'] |
|
| 28 | + * |
|
| 29 | + * @var ?string |
|
| 30 | + */ |
|
| 31 | + private ?string $tempDir; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * The Max count of chunk to download file |
|
| 35 | + * |
|
| 36 | + * @var int |
|
| 37 | + */ |
|
| 38 | + public int $maxChunkCount = 10; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The constructor of the client |
|
| 42 | + */ |
|
| 43 | + public function __construct() |
|
| 44 | + { |
|
| 45 | + $this->tempDir = $_SERVER['TEMP'] ?? null; |
|
| 46 | + $this->setHasSelfSignedCertificate(true); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Set has self-signed certificate |
|
| 51 | + * |
|
| 52 | + * This is used to set the curl option CURLOPT_SSL_VERIFYPEER |
|
| 53 | + * and CURLOPT_SSL_VERIFYHOST to false. This is useful when you are |
|
| 54 | + * in local environment, or you have self-signed certificate. |
|
| 55 | + * |
|
| 56 | + * @param bool $has |
|
| 57 | + * |
|
| 58 | + * @return void |
|
| 59 | + */ |
|
| 60 | + public function setHasSelfSignedCertificate(bool $has): void |
|
| 61 | + { |
|
| 62 | + putenv('HAS_SELF_SIGNED_CERT=' . ($has ? 'true' : 'false')); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Set the temporary directory path to save the downloaded files |
|
| 67 | + * |
|
| 68 | + * @param string $path |
|
| 69 | + * |
|
| 70 | + * @return void |
|
| 71 | + */ |
|
| 72 | + public function setTempPath(string $path): void |
|
| 73 | + { |
|
| 74 | + if (!file_exists($path)) { |
|
| 75 | + throw new InvalidArgumentException( |
|
| 76 | + sprintf('The path "%s" does not exist', $path) |
|
| 77 | + ); |
|
| 78 | + } |
|
| 79 | + $this->tempDir = $path; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * This method is used to send a http request to a given url. |
|
| 84 | + * |
|
| 85 | + * @param string $method |
|
| 86 | + * @param string $uri |
|
| 87 | + * @param array|HttpOptions $options |
|
| 88 | + * |
|
| 89 | + * @return HttpResponse |
|
| 90 | + */ |
|
| 91 | + public function request(string $method, string $uri, array|HttpOptions $options = []): HttpResponse |
|
| 92 | + { |
|
| 93 | + $CurlHandle = Middleware::create_curl_handler($method, $uri, $options); |
|
| 94 | + if (!$CurlHandle) { |
|
| 95 | + throw new RuntimeException('An error occurred while creating the curl handler'); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + $result = new HttpResponse(); |
|
| 99 | + $result->setCurlHandle($CurlHandle); |
|
| 100 | + |
|
| 101 | + $response = curl_exec($CurlHandle); |
|
| 102 | + if (curl_errno($CurlHandle) || !$response) { |
|
| 103 | + $result->setErrorCode(curl_errno($CurlHandle)); |
|
| 104 | + $result->setErrorMessage(curl_error($CurlHandle) ?? ErrorCode::getMessage(curl_errno($CurlHandle))); |
|
| 105 | + return $result; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $result->setStatusCode(curl_getinfo($CurlHandle, CURLINFO_HTTP_CODE)); |
|
| 109 | + $result->setHeaderSize(curl_getinfo($CurlHandle, CURLINFO_HEADER_SIZE)); |
|
| 110 | + $result->setHeaders(substr((string)$response, 0, $result->getHeaderSize())); |
|
| 111 | + $result->setBody(substr((string)$response, $result->getHeaderSize())); |
|
| 112 | + |
|
| 113 | + curl_close($CurlHandle); |
|
| 114 | + |
|
| 115 | + return $result; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Send multiple requests to a given url. |
|
| 120 | + * |
|
| 121 | + * @param array $requests [{method, uri, options}, ...] |
|
| 122 | + * |
|
| 123 | + * @return array<HttpResponse> |
|
| 124 | + */ |
|
| 125 | + public function bulk(array $requests): array |
|
| 126 | + { |
|
| 127 | + $result = []; |
|
| 128 | + $handlers = []; |
|
| 129 | + $multi_handler = curl_multi_init(); |
|
| 130 | + foreach ($requests as $request) { |
|
| 131 | + |
|
| 132 | + $CurlHandle = Middleware::create_curl_handler( |
|
| 133 | + $request['method'] ?? null, |
|
| 134 | + $request['uri'], |
|
| 135 | + $request['options'] ?? [] |
|
| 136 | + ); |
|
| 137 | + if (!$CurlHandle) { |
|
| 138 | + throw new RuntimeException( |
|
| 139 | + 'An error occurred while creating the curl handler' |
|
| 140 | + ); |
|
| 141 | + } |
|
| 142 | + $handlers[] = $CurlHandle; |
|
| 143 | + curl_multi_add_handle($multi_handler, $CurlHandle); |
|
| 144 | + |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + $active = null; |
|
| 148 | + do { |
|
| 149 | + $mrc = curl_multi_exec($multi_handler, $active); |
|
| 150 | + } while ($mrc == CURLM_CALL_MULTI_PERFORM); |
|
| 151 | + |
|
| 152 | + while ($active && $mrc == CURLM_OK) { |
|
| 153 | + if (curl_multi_select($multi_handler) != -1) { |
|
| 154 | + do { |
|
| 155 | + $mrc = curl_multi_exec($multi_handler, $active); |
|
| 156 | + } while ($mrc == CURLM_CALL_MULTI_PERFORM); |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + foreach ($handlers as $handler) { |
|
| 161 | + curl_multi_remove_handle($multi_handler, $handler); |
|
| 162 | + } |
|
| 163 | + curl_multi_close($multi_handler); |
|
| 164 | + |
|
| 165 | + foreach ($handlers as $handler) { |
|
| 166 | + $content = curl_multi_getcontent($handler); |
|
| 167 | + $response = new HttpResponse(); |
|
| 168 | + |
|
| 169 | + if (curl_errno($handler)) { |
|
| 170 | + $response->setErrorCode(curl_errno($handler)); |
|
| 171 | + $response->setErrorMessage( |
|
| 172 | + curl_error($handler) ?? |
|
| 173 | + ErrorCode::getMessage(curl_errno($handler)) |
|
| 174 | + ); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $response->setCurlHandle($handler); |
|
| 178 | + $response->setStatusCode(curl_getinfo($handler, CURLINFO_HTTP_CODE)); |
|
| 179 | + $response->setHeaderSize(curl_getinfo($handler, CURLINFO_HEADER_SIZE)); |
|
| 180 | + $response->setHeaders(substr($content, 0, $response->getHeaderSize())); |
|
| 181 | + $response->setBody(substr($content, $response->getHeaderSize())); |
|
| 182 | + |
|
| 183 | + $result[] = $response; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + return $result; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Download large files. |
|
| 191 | + * |
|
| 192 | + * This method is used to download large files with creating multiple requests. |
|
| 193 | + * |
|
| 194 | + * Change `max_chunk_count` variable to change the number of chunks. (default: 10) |
|
| 195 | + * |
|
| 196 | + * @param string $url The direct url to the file. |
|
| 197 | + * @param array|HttpOptions $options The options to use. |
|
| 198 | + * |
|
| 199 | + * @return DownloadResult |
|
| 200 | + */ |
|
| 201 | + public function download(string $url, array|HttpOptions $options = []): DownloadResult |
|
| 202 | + { |
|
| 203 | + if (empty($this->tempDir)) { |
|
| 204 | + throw new RuntimeException( |
|
| 205 | + 'The temp directory is not set. Please set the temp directory using the `setTempDir` method.' |
|
| 206 | + ); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + if (!file_exists($this->tempDir)) { |
|
| 210 | + if (mkdir($this->tempDir, 0777, true) === false) { |
|
| 211 | + throw new RuntimeException( |
|
| 212 | + 'The temp directory is not writable. Please set the temp directory using the `setTempDir` method.' |
|
| 213 | + ); |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if (gettype($options) === 'array') { |
|
| 218 | + $options = new HttpOptions($options); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $fileSize = $this->get_file_size($url); |
|
| 222 | + $chunkSize = $this->get_chunk_size($fileSize); |
|
| 223 | + |
|
| 224 | + $result = new DownloadResult(); |
|
| 225 | + |
|
| 226 | + $result->id = uniqid(); |
|
| 227 | + $result->chunksPath = $this->tempDir . '/' . $result->id . '/'; |
|
| 228 | + mkdir($result->chunksPath, 0777, true); |
|
| 229 | + |
|
| 230 | + $result->fileSize = $fileSize; |
|
| 231 | + $result->chunkSize = $chunkSize; |
|
| 232 | + $result->chunks = ceil($fileSize / $chunkSize); |
|
| 233 | + |
|
| 234 | + $result->startTime = time(); |
|
| 235 | + |
|
| 236 | + $requests = []; |
|
| 237 | + for ($i = 0; $i < $result->chunks; $i++) { |
|
| 238 | + $range = $i * $chunkSize . '-' . ($i + 1) * $chunkSize; |
|
| 239 | + if ($i + 1 === $result->chunks) { |
|
| 240 | + $range = $i * $chunkSize . '-' . $fileSize; |
|
| 241 | + } |
|
| 242 | + $requests[] = [ |
|
| 243 | + 'method' => 'GET', |
|
| 244 | + 'uri' => $url, |
|
| 245 | + 'options' => array_merge($options->toArray(), [ |
|
| 246 | + 'CurlOptions' => [ |
|
| 247 | + CURLOPT_RANGE => $range |
|
| 248 | + ], |
|
| 249 | + ]) |
|
| 250 | + ]; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + foreach ($this->bulk($requests) as $response) { |
|
| 254 | + $result->addChunk( |
|
| 255 | + Toolkit::randomString(16), |
|
| 256 | + $response->getBody(), |
|
| 257 | + $response->getInfoFromCurl()->TOTAL_TIME |
|
| 258 | + ); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + $result->endTime = time(); |
|
| 262 | + |
|
| 263 | + return $result; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Upload single or multiple files |
|
| 268 | + * |
|
| 269 | + * This method is sending file with request method of POST and |
|
| 270 | + * Content-Type of multipart/form-data. |
|
| 271 | + * |
|
| 272 | + * @param string $url The direct url to the file. |
|
| 273 | + * @param array $filePath The path to the file. |
|
| 274 | + * @param array|HttpOptions $options The options to use. |
|
| 275 | + * |
|
| 276 | + * @return HttpResponse |
|
| 277 | + */ |
|
| 278 | + public function upload(string $url, array $filePath, array|HttpOptions $options = []): HttpResponse |
|
| 279 | + { |
|
| 280 | + if (gettype($options) === 'array') { |
|
| 281 | + $options = new HttpOptions($options); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + $multipart = []; |
|
| 285 | + |
|
| 286 | + foreach ($filePath as $key => $file) { |
|
| 287 | + $multipart[$key] = new \CURLFile( |
|
| 288 | + realpath($file), |
|
| 289 | + $this->get_file_type($file) |
|
| 290 | + ); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + $options->setMultipart($multipart); |
|
| 294 | + return $this->post($url, $options); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * Get filetype with the extension. |
|
| 299 | + * |
|
| 300 | + * @param string $filename The absolute path to the file. |
|
| 301 | + * @return string eg. image/jpeg |
|
| 302 | + */ |
|
| 303 | + public static function get_file_type(string $filename): string |
|
| 304 | + { |
|
| 305 | + return MimeType::TYPES[pathinfo($filename, PATHINFO_EXTENSION)] ?? 'application/octet-stream'; |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Get file size. |
|
| 310 | + * |
|
| 311 | + * @param string $url The direct url to the file. |
|
| 312 | + * @return int |
|
| 313 | + */ |
|
| 314 | + public function get_file_size(string $url): int |
|
| 315 | + { |
|
| 316 | + if (file_exists($url)) { |
|
| 317 | + return filesize($url); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + $response = $this->get($url, [ |
|
| 321 | + 'headers' => [ |
|
| 322 | + 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36', |
|
| 323 | + ], |
|
| 324 | + 'CurlOptions' => [ |
|
| 325 | + CURLOPT_NOBODY => true, |
|
| 326 | + ] |
|
| 327 | + ]); |
|
| 328 | + |
|
| 329 | + return (int)$response->getHeaderLine('Content-Length') ?? 0; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Get the size of each chunk. |
|
| 334 | + * |
|
| 335 | + * For default, we're dividing filesize to 10 as max size of each chunk. |
|
| 336 | + * If the file size was smaller than 2MB, we'll use the filesize as single chunk. |
|
| 337 | + * |
|
| 338 | + * @param int $fileSize The file size. |
|
| 339 | + * @return int |
|
| 340 | + */ |
|
| 341 | + private function get_chunk_size(int $fileSize): int |
|
| 342 | + { |
|
| 343 | + $maxChunkSize = $fileSize / $this->maxChunkCount; |
|
| 344 | + |
|
| 345 | + if ($fileSize <= 2 * 1024 * 1024) { |
|
| 346 | + return $fileSize; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + return min($maxChunkSize, $fileSize); |
|
| 350 | + } |
|
| 351 | 351 | |
| 352 | 352 | } |
| 353 | 353 | \ No newline at end of file |
@@ -107,8 +107,8 @@ discard block |
||
| 107 | 107 | |
| 108 | 108 | $result->setStatusCode(curl_getinfo($CurlHandle, CURLINFO_HTTP_CODE)); |
| 109 | 109 | $result->setHeaderSize(curl_getinfo($CurlHandle, CURLINFO_HEADER_SIZE)); |
| 110 | - $result->setHeaders(substr((string)$response, 0, $result->getHeaderSize())); |
|
| 111 | - $result->setBody(substr((string)$response, $result->getHeaderSize())); |
|
| 110 | + $result->setHeaders(substr((string) $response, 0, $result->getHeaderSize())); |
|
| 111 | + $result->setBody(substr((string) $response, $result->getHeaderSize())); |
|
| 112 | 112 | |
| 113 | 113 | curl_close($CurlHandle); |
| 114 | 114 | |
@@ -326,7 +326,7 @@ discard block |
||
| 326 | 326 | ] |
| 327 | 327 | ]); |
| 328 | 328 | |
| 329 | - return (int)$response->getHeaderLine('Content-Length') ?? 0; |
|
| 329 | + return (int) $response->getHeaderLine('Content-Length') ?? 0; |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | /** |
@@ -12,57 +12,57 @@ |
||
| 12 | 12 | interface CommonsContract |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - // DADA types |
|
| 16 | - public const EVENT_TYPE_PING = 'ping'; |
|
| 17 | - public const EVENT_TYPE_PONG = 'pong'; |
|
| 18 | - public const EVENT_TYPE_TEXT = 'text'; |
|
| 19 | - public const EVENT_TYPE_CLOSE = 'close'; |
|
| 20 | - public const EVENT_TYPE_BINARY = 'binary'; |
|
| 21 | - public const EVENT_TYPE_CONTINUATION = 'continuation'; |
|
| 15 | + // DADA types |
|
| 16 | + public const EVENT_TYPE_PING = 'ping'; |
|
| 17 | + public const EVENT_TYPE_PONG = 'pong'; |
|
| 18 | + public const EVENT_TYPE_TEXT = 'text'; |
|
| 19 | + public const EVENT_TYPE_CLOSE = 'close'; |
|
| 20 | + public const EVENT_TYPE_BINARY = 'binary'; |
|
| 21 | + public const EVENT_TYPE_CONTINUATION = 'continuation'; |
|
| 22 | 22 | |
| 23 | - public const MAP_EVENT_TYPE_TO_METHODS = [ |
|
| 24 | - self::EVENT_TYPE_TEXT => 'onMessage', |
|
| 25 | - self::EVENT_TYPE_PING => 'onPing', |
|
| 26 | - self::EVENT_TYPE_PONG => 'onPong', |
|
| 27 | - ]; |
|
| 23 | + public const MAP_EVENT_TYPE_TO_METHODS = [ |
|
| 24 | + self::EVENT_TYPE_TEXT => 'onMessage', |
|
| 25 | + self::EVENT_TYPE_PING => 'onPing', |
|
| 26 | + self::EVENT_TYPE_PONG => 'onPong', |
|
| 27 | + ]; |
|
| 28 | 28 | |
| 29 | - // DECODE FRAMES |
|
| 30 | - public const DECODE_TEXT = 1; |
|
| 31 | - public const DECODE_BINARY = 2; |
|
| 32 | - public const DECODE_CLOSE = 8; |
|
| 33 | - public const DECODE_PING = 9; |
|
| 34 | - public const DECODE_PONG = 10; |
|
| 29 | + // DECODE FRAMES |
|
| 30 | + public const DECODE_TEXT = 1; |
|
| 31 | + public const DECODE_BINARY = 2; |
|
| 32 | + public const DECODE_CLOSE = 8; |
|
| 33 | + public const DECODE_PING = 9; |
|
| 34 | + public const DECODE_PONG = 10; |
|
| 35 | 35 | |
| 36 | - // ENCODE FRAMES |
|
| 37 | - public const ENCODE_TEXT = 129; |
|
| 38 | - public const ENCODE_CLOSE = 136; |
|
| 39 | - public const ENCODE_PING = 137; |
|
| 40 | - public const ENCODE_PONG = 138; |
|
| 36 | + // ENCODE FRAMES |
|
| 37 | + public const ENCODE_TEXT = 129; |
|
| 38 | + public const ENCODE_CLOSE = 136; |
|
| 39 | + public const ENCODE_PING = 137; |
|
| 40 | + public const ENCODE_PONG = 138; |
|
| 41 | 41 | |
| 42 | - // MASKS |
|
| 43 | - public const MASK_125 = 125; |
|
| 44 | - public const MASK_126 = 126; |
|
| 45 | - public const MASK_127 = 127; |
|
| 46 | - public const MASK_128 = 128; |
|
| 47 | - public const MASK_254 = 254; |
|
| 48 | - public const MASK_255 = 255; |
|
| 42 | + // MASKS |
|
| 43 | + public const MASK_125 = 125; |
|
| 44 | + public const MASK_126 = 126; |
|
| 45 | + public const MASK_127 = 127; |
|
| 46 | + public const MASK_128 = 128; |
|
| 47 | + public const MASK_254 = 254; |
|
| 48 | + public const MASK_255 = 255; |
|
| 49 | 49 | |
| 50 | - // PAYLOADS |
|
| 51 | - public const PAYLOAD_CHUNK = 8; |
|
| 52 | - public const PAYLOAD_MAX_BITS = 65535; |
|
| 50 | + // PAYLOADS |
|
| 51 | + public const PAYLOAD_CHUNK = 8; |
|
| 52 | + public const PAYLOAD_MAX_BITS = 65535; |
|
| 53 | 53 | |
| 54 | - // transfer protocol-level errors |
|
| 55 | - public const SERVER_COULD_NOT_BIND_TO_SOCKET = 101; |
|
| 56 | - public const SERVER_SELECT_ERROR = 102; |
|
| 57 | - public const SERVER_HEADERS_NOT_SET = 103; |
|
| 58 | - public const CLIENT_COULD_NOT_OPEN_SOCKET = 104; |
|
| 59 | - public const CLIENT_INCORRECT_SCHEME = 105; |
|
| 60 | - public const CLIENT_INVALID_UPGRADE_RESPONSE = 106; |
|
| 61 | - public const CLIENT_INVALID_STREAM_CONTEXT = 107; |
|
| 62 | - public const CLIENT_BAD_OPCODE = 108; |
|
| 63 | - public const CLIENT_COULD_ONLY_WRITE_LESS = 109; |
|
| 64 | - public const CLIENT_BROKEN_FRAME = 110; |
|
| 65 | - public const CLIENT_EMPTY_READ = 111; |
|
| 66 | - public const SERVER_INVALID_STREAM_CONTEXT = 112; |
|
| 54 | + // transfer protocol-level errors |
|
| 55 | + public const SERVER_COULD_NOT_BIND_TO_SOCKET = 101; |
|
| 56 | + public const SERVER_SELECT_ERROR = 102; |
|
| 57 | + public const SERVER_HEADERS_NOT_SET = 103; |
|
| 58 | + public const CLIENT_COULD_NOT_OPEN_SOCKET = 104; |
|
| 59 | + public const CLIENT_INCORRECT_SCHEME = 105; |
|
| 60 | + public const CLIENT_INVALID_UPGRADE_RESPONSE = 106; |
|
| 61 | + public const CLIENT_INVALID_STREAM_CONTEXT = 107; |
|
| 62 | + public const CLIENT_BAD_OPCODE = 108; |
|
| 63 | + public const CLIENT_COULD_ONLY_WRITE_LESS = 109; |
|
| 64 | + public const CLIENT_BROKEN_FRAME = 110; |
|
| 65 | + public const CLIENT_EMPTY_READ = 111; |
|
| 66 | + public const SERVER_INVALID_STREAM_CONTEXT = 112; |
|
| 67 | 67 | |
| 68 | 68 | } |
| 69 | 69 | \ No newline at end of file |
@@ -12,16 +12,16 @@ |
||
| 12 | 12 | interface ConnectionContract |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - public function send(string $data): void; |
|
| 15 | + public function send(string $data): void; |
|
| 16 | 16 | |
| 17 | - public function close(): void; |
|
| 17 | + public function close(): void; |
|
| 18 | 18 | |
| 19 | - public function getUniqueSocketId(): int; |
|
| 19 | + public function getUniqueSocketId(): int; |
|
| 20 | 20 | |
| 21 | - public function getPeerName(): string; |
|
| 21 | + public function getPeerName(): string; |
|
| 22 | 22 | |
| 23 | - public function broadCast(string $data): void; |
|
| 23 | + public function broadCast(string $data): void; |
|
| 24 | 24 | |
| 25 | - public function broadCastMany(array $data, int $delay): void; |
|
| 25 | + public function broadCastMany(array $data, int $delay): void; |
|
| 26 | 26 | |
| 27 | 27 | } |