0

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.

Iason
  • 372
  • 1
  • 5
  • 20
  • 1
    Your error is because you are using an outdated version of PHP relative to your version of Monolog. Either update to PHP 7.2 or downgrade your version of Monolog to 1.25.3. – lufc Apr 14 '20 at 20:18
  • 1
    ^^ more detail: Monolog 2.x declares a `public const` in Logger.php on line 33. This capability was only added in PHP 7.1. Technically Monolog 2.x requires PHP 7.2 but this specific error would be cleared by upgrading to 7.1. Safest bet is to downgrade Monolog. – lufc Apr 14 '20 at 20:22
  • Ah sh*t seriously ... , thanks for the tip and help guys. Going to try this out tomorrow. – Iason Apr 14 '20 at 20:30
  • Thanks @lufc downgrading from Logger 2.x to Logger 1.25.3 worked for me. – Iason Apr 15 '20 at 09:00

0 Answers0