* query必填成参数 * 发送多个请求 */
($url, $isPost = false, $params = [], $headers = [], $cookies = [])
| 106 | * 发送多个请求 |
| 107 | */ |
| 108 | static function guzzleMany($url, $isPost = false, $params = [], $headers = [], $cookies = []) |
| 109 | { |
| 110 | $client = new Client(); |
| 111 | //$url = 'https://tools.taokeduo.cn/a.php?'; |
| 112 | $requests = function () use ($url, $isPost, $params, $headers, $cookies) { |
| 113 | $total = count($params); |
| 114 | $headers = ['X-Foo' => 'Bar']; |
| 115 | for ($i = 0; $i < $total; $i++) { |
| 116 | if (!$isPost) { |
| 117 | $param = $params[$i]; |
| 118 | $wholeUrl = $url . "?"; |
| 119 | foreach ($param as $k => $v) { |
| 120 | $wholeUrl .= $k . '=' . (string)$v . '&'; |
| 121 | } |
| 122 | $url = rtrim($url, '&'); |
| 123 | yield new Request('GET', $wholeUrl,[]); |
| 124 | } else { |
| 125 | $body = [ |
| 126 | 'form_params' => $params[$i] |
| 127 | ]; |
| 128 | file_put_contents('1.txt', $url . '|' . json_encode($body) . PHP_EOL, FILE_APPEND); |
| 129 | $headers['Content-Type'] = "multipart/form-data"; |
| 130 | yield new Request('POST', $url, $headers, json_encode($body)); |
| 131 | } |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | $ret = []; |
| 136 | |
| 137 | $pool = new Pool($client, $requests(), [ |
| 138 | |
| 139 | 'concurrency' => 5, |
| 140 | |
| 141 | 'fulfilled' => function (Response $response, $index) use (&$ret) { |
| 142 | |
| 143 | // this is delivered each successful response |
| 144 | |
| 145 | $contents = $response->getBody(); |
| 146 | if ($contents instanceof Stream) { |
| 147 | $contents = $contents->getContents(); |
| 148 | } |
| 149 | $contents = json_decode($contents, true); |
| 150 | |
| 151 | $ret[$index] = $contents; |
| 152 | |
| 153 | }, |
| 154 | |
| 155 | 'rejected' => function (RequestException $reason, $index) { |
| 156 | |
| 157 | // this is delivered each failed request |
| 158 | |
| 159 | }, |
| 160 | |
| 161 | ]); |
| 162 | |
| 163 | // Initiate the transfers and create a promise |
| 164 | |
| 165 | $promise = $pool->promise(); |