Skip to content

Commit 8881a78

Browse files
committed
🚿 curl_multi example update
1 parent 06dd757 commit 8881a78

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

examples/curl_multi.php

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@
1010
* @license MIT
1111
*/
1212

13+
use chillerlan\HTTP\Utils\Query;
1314
use chillerlan\HTTP\CurlUtils\{CurlMultiClient, MultiResponseHandlerInterface};
1415
use chillerlan\HTTP\HTTPOptions;
1516
use chillerlan\HTTP\Psr18\CurlClient;
16-
use chillerlan\HTTP\Psr7\{Request, Query};
17+
use chillerlan\HTTP\Psr7\Request;
1718
use Psr\Http\Message\{RequestInterface, ResponseInterface};
18-
use function chillerlan\HTTP\Psr7\get_json;
19+
use function chillerlan\HTTP\Utils\get_json;
1920

2021
require_once __DIR__.'/../vendor/autoload.php';
2122

22-
// invoke the http clients
23+
// options for both clients
2324
$options = new HTTPOptions([
2425
'ca_info' => __DIR__.'/cacert.pem',
2526
'sleep' => 60 / 300 * 1000000, // GW2 API limit: 300 requests/minute
@@ -28,26 +29,15 @@
2829

2930
$client = new CurlClient($options);
3031

31-
// request the list of item ids
3232
$endpoint = 'https://api.guildwars2.com/v2/items';
33+
$languages = ['de', 'en', 'es'];//, 'fr', 'zh'
34+
// request the list of item ids
3335
$itemResponse = $client->sendRequest(new Request('GET', $endpoint));
3436

3537
if($itemResponse->getStatusCode() !== 200){
3638
exit('/v2/items fetch error');
3739
}
3840

39-
40-
// chunk the response into arrays of 200 ids each (API limit) and create Request objects for each desired language
41-
$languages = ['de', 'en', 'es'];//, 'fr', 'zh'
42-
$requests = [];
43-
44-
foreach(array_chunk(get_json($itemResponse), 200) as $chunk){
45-
foreach($languages as $lang){
46-
$requests[] = new Request('GET', $endpoint.'?'.Query::build(['lang' => $lang, 'ids' => implode(',', $chunk)]));
47-
}
48-
}
49-
50-
5141
// create directories for each language to dump the item responses into
5242
foreach($languages as $lang){
5343
$dir = __DIR__.'/'.$lang;
@@ -57,7 +47,6 @@
5747
}
5848
}
5949

60-
6150
// the multi request handler
6251
$handler = new class() implements MultiResponseHandlerInterface{
6352

@@ -86,10 +75,14 @@ public function handleResponse(ResponseInterface $response, RequestInterface $re
8675

8776
};
8877

78+
$multiClient = new CurlMultiClient($handler, $options);
8979

90-
// run the whole thing
91-
(new CurlMultiClient($handler, $options))
92-
->addRequests($requests)
93-
->process()
94-
;
80+
// chunk the item response into arrays of 200 ids each (API limit) and create Request objects for each desired language
81+
foreach(array_chunk(get_json($itemResponse), 200) as $chunk){
82+
foreach($languages as $lang){
83+
$multiClient->addRequest(new Request('GET', $endpoint.'?'.Query::build(['lang' => $lang, 'ids' => implode(',', $chunk)])));
84+
}
85+
}
9586

87+
// run the whole thing
88+
$multiClient->process();

0 commit comments

Comments
 (0)