1

i am new to gmb and i am trying to learn from trying and i don't know if this is the right approach to get data, i am trying to get data data from gmb but i am getting 500 error

<?php
include_once '../../google-api-php-client-2.4.0/vendor/autoload.php'; // or wherever autoload.php is located
include_once './MyBusiness.php';
ini_set('max_execution_time', '0');
include_once "../../includes/config.php";
$client = new Google_Client();
if (isset($_SERVER['HTTP_HOST']) && ($_SERVER['HTTP_HOST'] == "localhost")) {
    $client->setAuthConfig('../client_secret_for_Google_my_bussniss.json');
    $json = json_decode(file_get_contents('../Google-My-Business-tokens.json'));

}else{
    $client->setAuthConfig('/home/eblatech/insights-secrets/client_secret_for_Google_my_bussniss.json');
    $json = json_decode(file_get_contents('/home/eblatech/insights-secrets/Google-My-Business-tokens.json'));
}

$client->addScope('https://www.googleapis.com/auth/business.manage');
$refreshToken = $json->refreshToken;
$_SESSION['access_token']=$json->access_token;

// echo '<pre>'; print_r($refreshToken); echo '</pre>';exit;   
if (isset($refreshToken) && $refreshToken)  {
    $client->refreshToken($refreshToken);
    $mybusinessService = new Google_Service_Mybusiness($client);
    $posts = $mybusinessService->accounts_locations_localPosts;
    $x=$posts->get("BlackSwanLimos");
     echo '<pre>'; print_r($x); echo '</pre>';

update: i just opened error log and found this

19-Dec-2022 23:39:03 UTC] PHP Fatal error:  Uncaught Google_Service_Exception
[20-Dec-2022 00:00:02 UTC] PHP Notice:  Undefined index: HTTP_HOST in /home/eblatech/insights.ebla-tech.com/admin/includes/config.php on line 9
[20-Dec-2022 00:00:02 UTC] PHP Notice:  Undefined index: REQUEST_URI in /home/eblatech/insights.ebla-tech.com/admin/includes/config.php on line 9

that is the config.php

<?php
session_set_cookie_params(2*3600,"/");
session_start();
$APP_ROOT = "/insights-dashboard/admin/";
$FILES_ROOT = "/insights-dashboard/admin/files/";
$MODEL_ROOT = realpath(dirname(__FILE__) . '/../../');
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
define("SESSION", "insightEbla");
define("DEFAULT_LANGUAGE", "en");
// var_dump($_SESSION);
// exit;
if (!isset($_SESSION[SESSION]['lang']) || empty($_SESSION[SESSION]['lang'])) {
    $_SESSION[SESSION]['lang'] = DEFAULT_LANGUAGE;
};
$current_lang = $_SESSION[SESSION]['lang'];
if ($_SESSION[SESSION]['lang'] == 'ar') {
    require_once "lang/ar.php";
} else {
    require_once "lang/en.php";
}
require_once "page_names.php";
require_once "enum.php";
require_once  $MODEL_ROOT . '/model/db_endpoint.php';
require_once "authentication.php";
require_once "functions.php";
require_once "authorization.php";
require_once "pages_includes.php";
george derderian
  • 173
  • 2
  • 11
  • Does this answer your question? [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – ADyson Dec 19 '22 at 23:56
  • *" but i am getting 500 error"* - on your [tag:php] or the API you are calling? – Bagus Tesa Dec 20 '22 at 00:04
  • i am getting a blank page and when i open the inspect element i find it saying 500 – george derderian Dec 20 '22 at 00:08
  • It doesn't look like the code you provided is `config.php` which is where the error is coming from according to your logs. – Stephen Ostermiller Dec 20 '22 at 01:17
  • i edited the question with the config @StephenOstermiller – george derderian Dec 20 '22 at 01:23
  • The `500 error` is a very broad general error, but some clues should be found checking the webserver's error log. – Paul T. Dec 20 '22 at 03:33
  • Are you running this php script via a webserver or from the command line? – ADyson Dec 20 '22 at 08:55
  • web server @ADyson – george derderian Dec 20 '22 at 12:16
  • I see. It really makes no sense why those $_SERVER variables would be undefined, then. – ADyson Dec 20 '22 at 12:17
  • are you going to read the info related to Google Analytics or Google My business API? these are two different APIs with 2 different goals, and for both you need to have the business account and the related API active. Have a look at at this link for Google my business API: https://developers.google.com/my-business/content/prereqs. The error 500 might happen even if you haven't closed }. The codes you have provided are not complete. – Shila Mosammami Jan 11 '23 at 14:36

0 Answers0