How to fetch google reviews with rest API

In order to use GMB api. you have to request google to enable it by filling up a google form to them. Currently they only allow on one console project. For more interesting posts to read, you should check now the new post about the best bike bells 2023.

After they enable you need to configure oauth2 to get the relevant tokens. To enable oauth2 you need to create a project in google console and create credentials, Add the relevant callback redirect URL on which we will get the tokens. After that download the json file for credentials and put that in root directory. Google reviews can also be checked in social media with also other apps that work for your personal or commercial systems, using SocialBoosting is a method to improve your content and also grow your business.

Now we need PHP Google Client API package here is the link to get one https://github.com/googleapis/google-api-php-client

After you get their package via composer create an index.php file on the root folder and paste the below

include_once __DIR__ . '/vendor/autoload.php';
$google_client = new Google_Client();
$google_client->setAuthConfig('client_credentials.json');
$google_client->setRedirectUri('https://myrightcode.com/Fiverr/francescoriz769/callback.php');
$google_client->addScope('email');
$google_client->addScope('profile');
$google_client->setAccessType('offline');
$google_client->setApprovalPrompt('force');
$google_client->addScope('https://www.googleapis.com/auth/business.manage');
$google_client->addScope('https://www.googleapis.com/auth/plus.business.manage');
$response='';
if (isset($_GET['response'])){
    $response = '<p>'.$_GET['response'].'</p>';
}
<div class="col-md-3">
                    
                    <a class="btn btn-outline-dark" href="https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&client_id={client_id}&redirect_uri=https://abc.com/callback.php&state&scope=email profile https://www.googleapis.com/auth/business.manage https://www.googleapis.com/auth/plus.business.manage&approval_prompt=force" role="button" style="text-transform:none;font-size: 24px;">
                        <img width="30px" style="margin-bottom:3px; margin-right:5px" alt="Google sign-in" src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/512px-Google_%22G%22_Logo.svg.png">
                            Connect with Google
                    </a>
                </div>

After creating the above script you will be able to use it for oauth2 after that we need to create callback.php in root. Content of this file is below. Below file will save the tokens we receive from google into user_tokens.json on the same root directory.

include_once __DIR__ . '/vendor/autoload.php';

$google_client = new Google_Client();
$google_client->setAuthConfig('client_credentials.json');

if (isset($_GET['code'])) {
    $token = $google_client->fetchAccessTokenWithAuthCode($_GET['code']);

    $token_json = json_encode($token);
    
    $myfile = fopen("user_tokens.json", "w") or die("Unable to open file!");
    fwrite($myfile, $token_json);
    fclose($myfile);
}

Fetch GMB accounts

After that we would need to run accounts API because we need to get the account id first.

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://mybusiness.googleapis.com/v4/accounts/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json",
    "authorization: Bearer {access-token}",
    "cache-control: no-cache",
    "postman-token: 9491f4de-6418-0f9c-15b6-a9266bcc5e7b"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Above API will give json response and accounts/118287757********* the number you can see is your account id.

Fetch GMB locations

After that we would need to run the locations api to fetch locations. This will be needed to get the specific location id on which you wan’t to get the reviews list.

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://mybusiness.googleapis.com/v4/accounts/1182877**********473/locations/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json",
    "authorization: Bearer {access-token}",
    "cache-control: no-cache",
    "postman-token: a3878748-0bc0-30b9-42a4-90c86b427e30"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Above curl call will give you back all the locations in json format. In the name field “name”: “accounts/1182877************/locations/35*******426” last digit number is your location id.

Fetch reviews list

Now to fetch reviews you need to call the below API

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://mybusiness.googleapis.com/v4/accounts/{account_id}/locations/{location_id}/reviews",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json",
    "authorization: Bearer {access-token}",
    "cache-control: no-cache",
    "maxresults: 10",
    "postman-token: 22c00361-6b30-f829-9318-2e7ffebf9af3"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

The above curl call will give you the latest 50 reviews. In order to get all you would need to pass ?pageToken=ABHRLXVKs-*** , The next page token you will recieve when you hit the reviews API. Then just copy that and put into the URL with the param and it will give you next page reviews.

Renew Access token using refresh token

If you need to refresh the token you would need to run the below curl call to get new token. But remember that google gives refresh token only for one time so please save it you are not going to have it again you will get refresh token when you connect with google using oauth2 in that call you will get refresh token also. Later on you will use that refresh token in order to get access token again and again. When monetizing and improving the company with tools like pay stub generator to manage the finances.

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.googleapis.com/oauth2/v4/token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "client_id={client-id}&client_secret={client-secret}&refresh_token={refresh-token}&grant_type=refresh_token",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: application/x-www-form-urlencoded",
    "postman-token: 237e09b6-26c3-b6e4-570a-04d825567b03"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}