So I'm connected to the Google API by using the Google Library. Connections made with OAuth no problems there. So after that I want to connect the Google Search Console to retrieve some data. And apparently I am doing something wrong bit have figured it out jet.
1. So this it the code until now:
$redirect_uri = '[MYDEVURL]';
$client = new Google_Client();
$client->setAuthConfig('client_credentials.json');
$client->addScope(Google_Service_Webmasters::WEBMASTERS);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline'); // offline access
$client->setIncludeGrantedScopes(true); // incremental auth
$client->setLoginHint('MY EMAIL');
$client->setPrompt('consent');
if(isset($_GET['code']) || isset($_SESSION['access_token'])){
if(isset($_SESSION['access_token'])){
$access_token = $_SESSION['access_token'];
$client->setAccessToken($access_token);
}else{
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
$client->setAccessToken($access_token);
$_SESSION['access_token'] = $access_token;
}
$search = new Google_Service_Webmasters($client);
$postBody = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$postBody->setStartDate("2019-04-14");
$postBody->setEndDate("2020-04-14");
$postBody->setDimensions(["page","query"]);
$search->searchanalytics->query(urlencode('https://myurl.com'), $postBody);
exit;
} else {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
}
2. The problem is I think in this part:
$search = new Google_Service_Webmasters($client);
$postBody = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$postBody->setStartDate("2019-04-14");
$postBody->setEndDate("2020-04-14");
$postBody->setDimensions(["page","query"]);
$search->searchanalytics->query(urlencode('https://myurl.com'), $postBody);
3. It is giving me this erro:
Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE) in /my path on server/httpdocs/vendor/monolog/monolog/src/Monolog/Logger.php on line 33
I don't understand why this is happening apperently I am doing something wrong.
4. What I want to accomplish
I want to have the equavalent of the underneath when I use the api on the play ground. https://developers.google.com/oauthplayground/
Search Console API v3
Method: POST
URL: https://www.googleapis.com/webmasters/v3/sites/[myurl.com]/searchAnalytics/query
Body: {startDate:"2019-04-14", endDate:"2020-04-14", dimensions:["page","query"]}
Can anybody help me out please I'm stuck at this point.