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--