Here is my code to get the traffic stats from my Google Postmaster account. It works perfectly without the date parameters, but as soon as I add a startDate it doesn't work anymore
$url = 'https://gmailpostmastertools.googleapis.com/v1/domains/'.$this_domain.'/trafficStats/?pageSize=9999&startDate=20221001&endDate=20221030';
$ch = curl_init();
$headers = array(
'Authorization: Bearer ' . $token,
'Accept: application/json'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING , "");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$google_postmaster_api_json = curl_exec($ch);
$google_postmaster_api = json_decode($google_postmaster_api_json, true);
echo "<pre>";
var_dump($google_postmaster_api);
echo "</pre>";
if the StartDate parameter is added, the API returns this error:
Invalid JSON payload received. Unknown name "startDate": Cannot bind query parameter. 'startDate' is a message type. Parameters can only be bound to primitive types.
Invalid JSON payload received. Unknown name "endDate": Cannot bind query parameter. 'endDate' is a message type. Parameters can only be bound to primitive types.
If I try to POST it as a json instead, the result returned is NULL even if there should be data returned
$json_request = '{
"dateRanges": [{ "startDate": "2022-10-01", "endDate": "2022-10-15" }],
}';
$ch = curl_init();
$headers = array(
'Authorization: Bearer ' . $token,
'Accept: application/json'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_request);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING , "");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$google_postmaster_api_json = curl_exec($ch);