0

I have to do 40 requests to Google Analytics at the same time. But I have problems to understand the following batching explanation of GA to PHP (Link). I understand the purpose, but how do I translate this to PHP? I have no problem to do a usual cURL request, but how would it look like, when several requests are "nested" in a top request, like here? Is this possible with cURL? I am still new to PHP.

Without batching it takes too long to execute every request.

$authorization= 'Authorization: Bearer '.$google->accessToken;

$batch = curl_init(); 
curl_setopt_array($batch, array(
    CURLOPT_HTTPHEADER => array('Content-Type: multipart/mixed' , $authorization),
    CURLOPT_RETURNTRANSFER => TRUE,         
));

##...add nested requests here somewhere....

$result = curl_exec($batch);
curl_close($batch);

Google example:

POST /batch HTTP/1.1

Host: www.googleapis.com

Content-length: 731

Content-type: multipart/mixed; boundary=batch_0123456789

Authorization: Bearer ya29.5gFZooleNoSpGqYOOF0eFciUGz1x26k9GagZoW7HJCogWlCoNOotxlZPo7bDbwo1ykDq

--batch_0123456789

Content-Type: application/http

Content-ID:

Content-Transfer-Encoding: binary

POST [https]://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-1/customDimensions

Content-Type: application/json

Content-Length: 68

{ "name": "Campaign Group", "scope": "SESSION", "active": true }

--batch_0123456789

Content-Type: application/http

Content-ID:

Content-Transfer-Encoding: binary

POST [https]://www.googleapis.com/analytics/v3/management/accounts/XXXXXX/webproperties/UA-XXXXXX-1/customDimensions

Content-Type: application/json

Content-Length: 67

{ "name": "Campaign Type", "scope": "SESSION", "active": true }

--batch_0123456789--

André GC
  • 51
  • 1
  • 5
  • Even if you do not want to use the Google PHP client library you can look at the code (https://github.com/google/google-api-php-client/blob/master/src/Google/Http/Batch.php) to see how Google does it (they use the Guzzle http client instead of pure Curl but I guess one could apply similar principles for Curl. Or simply use the Google libs). – Eike Pierstorff Nov 19 '15 at 21:14
  • [Client Library documentation](https://developers.google.com/api-client-library/php/guide/batch) For those who want to go the easy route. – Matt Nov 19 '15 at 22:52
  • Link non-existing. In github, you can only find the reference, not a guide. https://googleapis.github.io/google-api-php-client/master/Google/Http/Batch.html. So, I guess no easy route in the end. – Isidro Moran Feb 09 '21 at 21:17
  • https://googleapis.github.io/google-api-php-client/master/Google/Http/Batch.html Just reference on GitHub. No guide :( – Isidro Moran Feb 09 '21 at 21:18

0 Answers0