如何在不使用代理的情况下通过 guzzle 发送异步请求
How to send asynchronous requests by guzzle without using proxy
我正在尝试通过 php guzzle 向 Instagram 网站异步发送 100 个请求,如下所示:
$client = new Client();
$promises = [];
for($i = 1; $i <= 100; $i++)
{
$options = ['timeout' => 60];
$promise = $client->getAsync("https://www.instagram.com/p/B7IoQYhACcF/", $options)
->then(function($response){
return $response;
}, function($exception){
return [
'msg' => $exception->getMessage(),
];
});
$promises[$i] = $promise;
}
之后我尝试打印请求状态:
foreach ($promises as $key => $promise)
{
$response = $promise->wait();
if(is_array($response))
{
echo "{$key}) error: {$response['msg']} <br/>";
continue;
}
echo "{$key}) {$response->getStatusCode()} {$response->getReasonPhrase()} <br/>";
}
好的!但是这里的问题是什么?
问题是只有前 20 个(或更多)请求 ok 而其他请求未解决。
这段代码的结果在这里:
1) 200 OK
2) 200 OK
3) 200 OK
4) 200 OK
5) 200 OK
6) 200 OK
7) 200 OK
8) 200 OK
9) 200 OK
10) 200 OK
11) 200 OK
12) 200 OK
13) 200 OK
14) 200 OK
15) 200 OK
16) 200 OK
17) 200 OK
18) 200 OK
19) 200 OK
20) 200 OK
21) 200 OK
22) 200 OK
23) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
24) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
25) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
26) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
27) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
28) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
29) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
30) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
31) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
32) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
33) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
34) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
35) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
36) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
37) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
38) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
39) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
40) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
41) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
42) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
43) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
44) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
45) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
46) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
47) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
48) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
49) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
50) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
51) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
52) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
53) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
54) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
55) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
56) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
57) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
58) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
59) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
60) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
61) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
62) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
63) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
64) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
65) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
66) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
67) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
68) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
69) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
70) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
71) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
72) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
73) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
74) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
75) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
76) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
77) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
78) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
79) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
80) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
81) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
82) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
83) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
84) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
85) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
86) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
87) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
88) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
89) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
90) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
91) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
92) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
93) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
94) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
95) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
96) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
97) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
98) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
99) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
100) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
请注意,我通过使用 http 代理解决了这个问题但是我不能使用任何代理来做到这一点。
$options = ['timeout' => 60, 'proxy' => '151.253.165.70:8080'];
我想当我使用 proxy 发送请求时,一些 guzzle(或 cURL 库)选项发生了变化。
最后我的问题是如何在不使用任何代理的情况下发送 100 个异步请求?????
Guzzle afaik 在后台使用的函数 curl_multi() 一次只允许特定数量的并行请求。你可能想像这样配置更高的数字
curl_multi_setopt($curlHandle, CURLMOPT_MAXCONNECTS, 100)
and/or 查看其他 curl multi options here.
现在您可能想知道如何在 guzzle 使用的卷曲手柄上设置该选项。 According to this 您可以将 curl 选项作为数组传递给 Handlerstack。
链接文档摘录:
use \GuzzleHttp\Client;
use \GuzzleHttp\HandlerStack;
use \GuzzleHttp\Handler\CurlMultiHandler;
$client = new Client(['handler' => HandlerStack::create(new CurlMultiHandler([
'options' => [
CURLMOPT_MAX_TOTAL_CONNECTIONS => 50,
CURLMOPT_MAX_HOST_CONNECTIONS => 5,
]
]))]);
请注意,示例中提到的选项也可能与您的情况相关。
我正在尝试通过 php guzzle 向 Instagram 网站异步发送 100 个请求,如下所示:
$client = new Client();
$promises = [];
for($i = 1; $i <= 100; $i++)
{
$options = ['timeout' => 60];
$promise = $client->getAsync("https://www.instagram.com/p/B7IoQYhACcF/", $options)
->then(function($response){
return $response;
}, function($exception){
return [
'msg' => $exception->getMessage(),
];
});
$promises[$i] = $promise;
}
之后我尝试打印请求状态:
foreach ($promises as $key => $promise)
{
$response = $promise->wait();
if(is_array($response))
{
echo "{$key}) error: {$response['msg']} <br/>";
continue;
}
echo "{$key}) {$response->getStatusCode()} {$response->getReasonPhrase()} <br/>";
}
好的!但是这里的问题是什么?
问题是只有前 20 个(或更多)请求 ok 而其他请求未解决。
这段代码的结果在这里:
1) 200 OK
2) 200 OK
3) 200 OK
4) 200 OK
5) 200 OK
6) 200 OK
7) 200 OK
8) 200 OK
9) 200 OK
10) 200 OK
11) 200 OK
12) 200 OK
13) 200 OK
14) 200 OK
15) 200 OK
16) 200 OK
17) 200 OK
18) 200 OK
19) 200 OK
20) 200 OK
21) 200 OK
22) 200 OK
23) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
24) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
25) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
26) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
27) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
28) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
29) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
30) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
31) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
32) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
33) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
34) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
35) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
36) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
37) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
38) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
39) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
40) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
41) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
42) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
43) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
44) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
45) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
46) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
47) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
48) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
49) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
50) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
51) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
52) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
53) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
54) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
55) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
56) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
57) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
58) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
59) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
60) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
61) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
62) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
63) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
64) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
65) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
66) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
67) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
68) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
69) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
70) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
71) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
72) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
73) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
74) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
75) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
76) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
77) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
78) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
79) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
80) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
81) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
82) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
83) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
84) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
85) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
86) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
87) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
88) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
89) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
90) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
91) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
92) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
93) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
94) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
95) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
96) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
97) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
98) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
99) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
100) error: cURL error 6: (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)
请注意,我通过使用 http 代理解决了这个问题但是我不能使用任何代理来做到这一点。
$options = ['timeout' => 60, 'proxy' => '151.253.165.70:8080'];
我想当我使用 proxy 发送请求时,一些 guzzle(或 cURL 库)选项发生了变化。
最后我的问题是如何在不使用任何代理的情况下发送 100 个异步请求?????
Guzzle afaik 在后台使用的函数 curl_multi() 一次只允许特定数量的并行请求。你可能想像这样配置更高的数字
curl_multi_setopt($curlHandle, CURLMOPT_MAXCONNECTS, 100)
and/or 查看其他 curl multi options here.
现在您可能想知道如何在 guzzle 使用的卷曲手柄上设置该选项。 According to this 您可以将 curl 选项作为数组传递给 Handlerstack。
链接文档摘录:
use \GuzzleHttp\Client;
use \GuzzleHttp\HandlerStack;
use \GuzzleHttp\Handler\CurlMultiHandler;
$client = new Client(['handler' => HandlerStack::create(new CurlMultiHandler([
'options' => [
CURLMOPT_MAX_TOTAL_CONNECTIONS => 50,
CURLMOPT_MAX_HOST_CONNECTIONS => 5,
]
]))]);
请注意,示例中提到的选项也可能与您的情况相关。