File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,25 @@ var_dump($result->getSpeedScore()); // 100
2121var_dump($result->getUsabilityScore()); // 100
2222```
2323
24+ ### Using Concurrent Requests
25+ ``` php
26+ $urls = array(
27+ 'http://example.com',
28+ 'http://example2.com',
29+ 'http://example3.com'
30+ );
31+
32+ $caller = new \PhpInsights\InsightsCaller('your-google-api-key-here', 'fr');
33+ $responses = $caller->getResponses($urls, \PhpInsights\InsightsCaller::STRATEGY_MOBILE);
34+
35+ foreach ($responses as $url=>$response) {
36+ $result = $response->getMappedResult();
37+
38+ var_dump($result->getSpeedScore()); // 100
39+ var_dump($result->getUsabilityScore()); // 100
40+ }
41+ ```
42+
2443### Result details
2544#### Full result
2645``` php
Original file line number Diff line number Diff line change 22namespace PhpInsights ;
33
44use GuzzleHttp \Client ;
5+ use GuzzleHttp \Promise ;
56use GuzzleHttp \Exception \TransferException ;
67
78class InsightsCaller
@@ -74,6 +75,44 @@ public function getResponse($url, $strategy = 'mobile')
7475
7576 }
7677
78+ /**
79+ * @param array $urls
80+ * @param string $strategy
81+ *
82+ * @return InsightsResponse
83+ *
84+ * @throws ApiRequestException
85+ */
86+ public function getResponses (array $ urls , $ strategy = 'mobile ' )
87+ {
88+
89+ try {
90+ $ promises = array ();
91+
92+ foreach ($ urls as $ k =>$ url ) {
93+ $ apiEndpoint = $ this ->createApiEndpointUrl ($ url , $ strategy );
94+ $ promises [$ k ] = $ this ->client ->getAsync ($ apiEndpoint );
95+ }
96+
97+ $ results = Promise \unwrap ($ promises );
98+ $ results = Promise \settle ($ promises )->wait ();
99+
100+ $ responses = array ();
101+
102+ foreach ($ urls as $ k =>$ url ) {
103+ $ response = $ results [$ k ]['value ' ];
104+ $ responses [$ url ] = InsightsResponse::fromResponse ($ response );
105+ }
106+
107+
108+ } catch (TransferException $ e ) {
109+ throw new ApiRequestException ($ e ->getMessage ());
110+ }
111+
112+ return $ responses ;
113+
114+ }
115+
77116 /**
78117 * @return boolean
79118 */
You can’t perform that action at this time.
0 commit comments