How to get Locations list in Google My Business API | PHP

Viewed 1661

How can I get locations list in Google My Business API. Where I retrieved account list but I can't figure out how to retrieve location.

Here is my code where I am getting accounts list

define('GOOGLE_CLIENT_ID', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
define('GOOGLE_CLIENT_SECRET', 'XXXXXXXXXXXXX');

// Create Client Request to access Google API
$client = new Client();
$client->setApplicationName('my-app');
$client->setClientId(GOOGLE_CLIENT_ID);
$client->setClientSecret(GOOGLE_CLIENT_SECRET);
$client->setRedirectUri('https://example.com/callback');
$client->addScope('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/business.manage');
$client->setAccessType('offline');        // offline access
$client->setIncludeGrantedScopes(true);   // incremental auth

$client->setAccessToken($accessToken);

$service = new \Google_Service_MyBusinessAccountManagement($client);
$accounts = $service->accounts->listAccounts()->getAccounts(); // get accounts
2 Answers

To get google locations,
you can use this PHP My Business file to make things little easier.

First change you scope to ttps://www.googleapis.com/auth/plus.business.manage then include the file and create a object of Google_Service_MyBusiness with you client and then do like this.

 $mybusinessService = new Google_Service_MyBusiness($client);
 // Get the first account in the accounts array
    $accounts = $mybusinessService->accounts;
    $accountsList = $accounts->listAccounts()->getAccounts();
    $account = $accountsList[0];


    // Get the first location in the locations array
    $locations = $mybusinessService->accounts_locations;
    $locationsList = $locations->listAccountsLocations($account->name)->getLocations();
    $location = $locationsList[0];
    var_export($location);

With this process you can also able to get google reviews.

For more details check this Google Business API documentation.

Related