How to use Google MyBusiness API in php

Bhavesh Nariya picture Bhavesh Nariya · Jan 25, 2017 · Viewed 9.4k times · Source

I am using Google MyBsiness API for fetching all business reviews. But I am unable to familer with PHP syntax and GET, POST method use in MYBusiness.

After Oath here are code i am using to fetch review

$mybusinessService = new Google_Service_Mybusiness($client);
$accessToken = file_get_contents($credentialsPath);

$reviews = $mybusinessService->accounts_locations_reviews;
echo '<pre>';print_r($reviews->get('ArtechDev'));exit;

But i am getting error 404 (Fatal error: Uncaught exception 'Google_Service_Exception' with message)

I am sure that I don't know how to pass param and which things needed for it. I am logged in as account which having Location 'ArtechDev' also please let me know that where can i call

https://mybusiness.googleapis.com/v3/accounts/account_name/locations/location_name/reviews

Thanks

Answer

Angie picture Angie · Feb 22, 2018

I hope you had found the answer to your question a long time ago, anyway I'll leave this hoping help someone else.

/*$accounts previusly populate*/
/*(GMB - v4)*/
$credentials_f = "google_my_business_credentials_file.json";
$client = new Google_Client();
$client->setApplicationName($aplicattion_name);
$client->setDeveloperKey($developer_key);
$client->setAuthConfig($credentials_f);  
$client->setScopes("https://www.googleapis.com/auth/plus.business.manage");
$client->setSubject($accounts->email);   
$token = $client->refreshToken($accounts->refresh_token);
$client->authorize();

$locationName = "accounts/#######/locations/########";

$mybusinessService = new Google_Service_Mybusiness($client);

$reviews = $mybusinessService->accounts_locations_reviews;

do{
    $listReviewsResponse = $reviews->listAccountsLocationsReviews($locationName, array('pageSize' => 100,
                        'pageToken' => $listReviewsResponse->nextPageToken));

    $reviewsList = $listReviewsResponse->getReviews();
    foreach ($reviewsList as $index => $review) {
        /*Accesing $review Object

        * $review->createTime;
        * $review->updateTime;
        * $review->starRating;
        * $review->reviewer->displayName;
        * $review->reviewReply->comment;
        * $review->getReviewReply()->getComment();
        * $review->getReviewReply()->getUpdateTime();
        */

    }

}while($listReviewsResponse->nextPageToken);