1010 * @license MIT
1111 */
1212
13+ use chillerlan \HTTP \{CurlClient , CurlMultiClient , HTTPOptions , MultiResponseHandlerInterface };
14+ use chillerlan \HTTP \Psr7 \HTTPFactory ;
1315use chillerlan \HTTP \Utils \{MessageUtil , QueryUtil };
14- use chillerlan \HTTP \Common \{CurlMultiClient , MultiResponseHandlerInterface };
15- use chillerlan \HTTP \HTTPOptions ;
16- use chillerlan \HTTP \Psr18 \CurlClient ;
17- use chillerlan \HTTP \Psr7 \Request ;
1816use Psr \Http \Message \{RequestInterface , ResponseInterface };
1917
2018require_once __DIR__ .'/../vendor/autoload.php ' ;
2119
2220// options for both clients
2321$ options = new HTTPOptions ;
2422$ options ->ca_info = __DIR__ .'/cacert.pem ' ;
25- $ options ->sleep = ( 60 / 300 * 1000000 ) ; // GW2 API limit: 300 requests/minute
23+ $ options ->sleep = 1 ; // GW2 API limit: 300 requests/minute
2624#$options->user_agent = 'my fancy http client';
2725
28- $ client = new CurlClient ($ options );
26+ $ factory = new HTTPFactory ;
27+ $ client = new CurlClient ($ factory , $ options );
2928
3029$ endpoint = 'https://api.guildwars2.com/v2/items ' ;
3130$ languages = ['de ' , 'en ' , 'es ' ];//, 'fr', 'zh'
3231// request the list of item ids
33- $ itemResponse = $ client ->sendRequest (new Request ('GET ' , $ endpoint ));
32+ $ itemResponse = $ client ->sendRequest ($ factory -> createRequest ('GET ' , $ endpoint ));
3433
3534if ($ itemResponse ->getStatusCode () !== 200 ){
3635 exit ('/v2/items fetch error ' );
@@ -63,7 +62,7 @@ public function handleResponse(ResponseInterface $response, RequestInterface $re
6362 try {
6463 $ json = MessageUtil::decodeJSON ($ response );
6564 }
66- catch (Throwable $ e ){
65+ catch (Throwable ){
6766 // maybe we didn't properly receive the data? let's try again
6867 return $ request ;
6968 }
@@ -73,7 +72,7 @@ public function handleResponse(ResponseInterface $response, RequestInterface $re
7372 $ file = $ lang .'/ ' .$ item ->id ;
7473 file_put_contents (__DIR__ .'/ ' .$ file .'.json ' , json_encode ($ item , (JSON_PRETTY_PRINT |JSON_UNESCAPED_SLASHES )));
7574
76- # echo $file.PHP_EOL;
75+ echo $ file .PHP_EOL ;
7776 }
7877
7978 // response ok, nothing to return
@@ -86,12 +85,12 @@ public function handleResponse(ResponseInterface $response, RequestInterface $re
8685
8786};
8887
89- $ multiClient = new CurlMultiClient ($ handler , $ options );
88+ $ multiClient = new CurlMultiClient ($ handler , $ factory , $ options );
9089
9190// chunk the item response into arrays of 200 ids each (API limit) and create Request objects for each desired language
9291foreach (array_chunk (MessageUtil::decodeJSON ($ itemResponse ), 200 ) as $ chunk ){
9392 foreach ($ languages as $ lang ){
94- $ multiClient ->addRequest (new Request ('GET ' , $ endpoint .'? ' .QueryUtil::build (['lang ' => $ lang , 'ids ' => implode (', ' , $ chunk )])));
93+ $ multiClient ->addRequest ($ factory -> createRequest ('GET ' , $ endpoint .'? ' .QueryUtil::build (['lang ' => $ lang , 'ids ' => implode (', ' , $ chunk )])));
9594 }
9695}
9796
0 commit comments