|
10 | 10 | * @license MIT |
11 | 11 | */ |
12 | 12 |
|
| 13 | +use chillerlan\HTTP\Utils\Query; |
13 | 14 | use chillerlan\HTTP\CurlUtils\{CurlMultiClient, MultiResponseHandlerInterface}; |
14 | 15 | use chillerlan\HTTP\HTTPOptions; |
15 | 16 | use chillerlan\HTTP\Psr18\CurlClient; |
16 | | -use chillerlan\HTTP\Psr7\{Request, Query}; |
| 17 | +use chillerlan\HTTP\Psr7\Request; |
17 | 18 | use Psr\Http\Message\{RequestInterface, ResponseInterface}; |
18 | | -use function chillerlan\HTTP\Psr7\get_json; |
| 19 | +use function chillerlan\HTTP\Utils\get_json; |
19 | 20 |
|
20 | 21 | require_once __DIR__.'/../vendor/autoload.php'; |
21 | 22 |
|
22 | | -// invoke the http clients |
| 23 | +// options for both clients |
23 | 24 | $options = new HTTPOptions([ |
24 | 25 | 'ca_info' => __DIR__.'/cacert.pem', |
25 | 26 | 'sleep' => 60 / 300 * 1000000, // GW2 API limit: 300 requests/minute |
|
28 | 29 |
|
29 | 30 | $client = new CurlClient($options); |
30 | 31 |
|
31 | | -// request the list of item ids |
32 | 32 | $endpoint = 'https://api.guildwars2.com/v2/items'; |
| 33 | +$languages = ['de', 'en', 'es'];//, 'fr', 'zh' |
| 34 | +// request the list of item ids |
33 | 35 | $itemResponse = $client->sendRequest(new Request('GET', $endpoint)); |
34 | 36 |
|
35 | 37 | if($itemResponse->getStatusCode() !== 200){ |
36 | 38 | exit('/v2/items fetch error'); |
37 | 39 | } |
38 | 40 |
|
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 | | - |
51 | 41 | // create directories for each language to dump the item responses into |
52 | 42 | foreach($languages as $lang){ |
53 | 43 | $dir = __DIR__.'/'.$lang; |
|
57 | 47 | } |
58 | 48 | } |
59 | 49 |
|
60 | | - |
61 | 50 | // the multi request handler |
62 | 51 | $handler = new class() implements MultiResponseHandlerInterface{ |
63 | 52 |
|
@@ -86,10 +75,14 @@ public function handleResponse(ResponseInterface $response, RequestInterface $re |
86 | 75 |
|
87 | 76 | }; |
88 | 77 |
|
| 78 | +$multiClient = new CurlMultiClient($handler, $options); |
89 | 79 |
|
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 | +} |
95 | 86 |
|
| 87 | +// run the whole thing |
| 88 | +$multiClient->process(); |
0 commit comments