File tree 2 files changed +58
-0
lines changed
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
21
21
var_dump($result->getUsabilityScore()); // 100
22
22
```
23
23
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
+
24
43
### Result details
25
44
#### Full result
26
45
``` php
Original file line number Diff line number Diff line change 2
2
namespace PhpInsights ;
3
3
4
4
use GuzzleHttp \Client ;
5
+ use GuzzleHttp \Promise ;
5
6
use GuzzleHttp \Exception \TransferException ;
6
7
7
8
class InsightsCaller
@@ -74,6 +75,44 @@ public function getResponse($url, $strategy = 'mobile')
74
75
75
76
}
76
77
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
+
77
116
/**
78
117
* @return boolean
79
118
*/
You can’t perform that action at this time.
0 commit comments