Here we are going to Learn how to use the Google My Business API to better optimize your Google My Business account. API description and information is provided. We will discuss how to manage google business, locations and reviews using Google My Business Api OR (GMB API ) using PHP.
Google My Business
Customers who search locally in Google Search or Maps find Google My Business listings right where they search. Google My Business provides a fast and easy way for businesses, products, brands, artists, and organizations to manage their online presence with Google. Listings are also where customers learn about a business, leave reviews, and share pictures of their experience with products or services.
Google My Business API
The Google My Business API is a programming interface that allows developers to write applications to manage their Google My Business account and location data.
The API allows merchants or their representatives to manage how their data is presented across Google and control who co-manages their data. User-created data, such as photos, posts, and reviews, can be managed through the API.
The API supports a subset of the features available on the Google My Business interface. For the features that are available in the API, all functionality and quality guidelines are the same as that of the full interface.
API functionality
The Google My Business API offers much of the same functionality as the Google My Business user interface, plus some additional features specific to the API. Users of the API can expect Google updates to occur just as they do for users of the user interface.
The API enables you to programmatically create and edit locations in Google My Business. Here are some of the features you can manage through the API:
- Create business locations with information such as name, address, phone number, category, business hours and more.
- Manage special hours.
- Mark a business location as permanently closed.
- Manage business photos.
- List, invite and remove managers on locations and business accounts.
- Read listing state to identify Google updated, duplicate and suspended locations.
- Search/Filter locations by name, category and label.
- Set the service area for a business by specifying a point and radius or Place IDs.
This API is typically aimed at larger businesses that need to manage many locations and have special holiday hours. It may also be used by marketing agencies looking to manage their clients.
The GMB API is a way for businesses to input their information directly into Google Search, Google Maps, Waze, and Google Ads. A GMB account owner can create and edit business locations, meaning that the business itself can input data, as well as control how business will appear in search results. The locations entered into GMB can be used in Google Ads, but they cannot appear in Google Search,Google Maps, or Waze unless they have first been verified. Verification is NOT supported through the API.
Google is always improving its API and as the use of intelligent services and devices increases, it’ll be increasingly important for businesses to keep track of updates so they can be the first to take advantage.
GMB API V3.1
In September 2016, Google launched the V3.1 update. The V3.1 update included the addition of numerous location attributes that businesses can add to their Google listings to better describe what they have to offer. For example, a restaurant can denote whether or not it takes reservations, a supermarket can indicate whether it carries organic products, so on and so forth. The update also enabled GMB account owners to set up real-time notifications for when listings were updated and a new review was written about one of their locations.
GMB API V3.2
Google released the V3.2 GMB API in February 2017. This API update made GMB Analytics available via the GMB API. Insights — from Phone Calls and Driving Directions to Search and Map Views — help businesses track and analyze where and how consumers find and interact with them on Google. Knowing these kinds of metrics can help businesses make educated operational, planning, and marketing decisions.
How to Start with Google My Business API Using PHP
First of all, you should make sure that you installed the v1-branch of the repo for the Google API Client Library for PHP on GitHub or via Composer as instructed on the Installation page. After obtaining the library files, you should include the autoloader in your application. I notice that you included the source code of the PHP client library for the Google My Business API in your application from your first post of this topic. You should just use the include statement to include and evaluate the specified “Mybusiness.php” file.
Please follow the code snippet below for using an OAuth 2.0 client ID for OAuth 2.0 authorization and persisting the refresh token with the Google API Client Library for PHP (Check out this thread for more help):
require_once '/path/to/google-api-php-client/vendor/autoload.php'; include_once "Mybusiness.php"; define('APPLICATION_NAME', 'User Query - Google My Business API'); define('CREDENTIALS_PATH', '/path/to/credentials.json'); define('CLIENT_SECRET_PATH', '/path/to/client_secrets.json'); $redirect_uri = '<YOUR_REDIRECT_URI>'; $client = new Google_Client(); $client->setApplicationName(APPLICATION_NAME); $client->setAuthConfigFile(CLIENT_SECRET_PATH); $client->addScope("https://www.googleapis.com/auth/plus.business.manage"); $client->setRedirectUri($redirect_uri); // For retrieving the refresh token $client->setAccessType("offline"); $client->setApprovalPrompt("force"); /************************************************ We are going to create the Google My Business API service, and query it. ************************************************/ $mybusinessService = new Google_Service_Mybusiness($client); $credentialsPath = CREDENTIALS_PATH; if (isset($_GET['code'])) { // Exchange authorization code for an access token. $accessToken = $client->authenticate($_GET['code']); // Store the credentials to disk. if (!file_exists(dirname($credentialsPath))) { mkdir(dirname($credentialsPath), 0700, true); } file_put_contents($credentialsPath, $accessToken); $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); } // Load previously authorized credentials from a file. if (file_exists($credentialsPath)) { $accessToken = file_get_contents($credentialsPath); $client->setAccessToken($accessToken); // Refresh the token if it's expired. if ($client->isAccessTokenExpired()) { $client->refreshToken($client->getRefreshToken()); file_put_contents($credentialsPath, $client->getAccessToken()); } } else { // Request authorization from the user. $authUrl = $client->createAuthUrl(); } // For testing purposes, selects the very first account in the accounts array $accounts = $mybusinessService->accounts; $accountsList = $accounts->listAccounts()->getAccounts(); $account = $accountsList[0]; // For testing purposes, selects the very first location in the locations array $locations = $mybusinessService->accounts_locations; $locationsList = $locations->listAccountsLocations($account->name)->getLocations(); $location = $locationsList[0]; // Lists all reviews for the specified location $reviews = $mybusinessService->accounts_locations_reviews; $listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name); $reviewsList = $listReviewsResponse->getReviews(); print '<pre>' . json_encode($reviewsList, JSON_PRETTY_PRINT) . '</pre>'; print '<pre>' . json_encode($listReviewsResponse, JSON_PRETTY_PRINT) . '</pre>';
Please also check out this Accepted Solution for setting up a service account with v1-branch of the Google API Client Library for PHP and the PHP client library.
The PHP client library doesn’t currently support the display of detailed error messages. Please follow the Make a simple HTTP request step-by-step guide on the Google Developers site to learn how to use the OAuth 2.0 Playground for troubleshooting errors with detailed error messages when sending requests with data via the Google My Business API.
There are plenty of resources for using the PHP client library on this board. When you search in the Google My Business API community for the answer, please use the search box at the top of the page and filter your search results “By location” and select the “Google My Business API” board. This will limit your search results to the topics created in the Google My Business API board only. You can click on “Advanced Search…” to the right of the search box on the search results page to view results by “Specific posts” for the answer found in posts under a topic.
You can learn about how to list all paginated locations by including the pageSize and pageToken query parameters with the PHP client library in this thread.
You can learn about how to create a new location with the PHP client library in this thread.
You can learn about how to update a location with the PHP client library in this thread.
4 Comments
Vikas
How will be the format for this file /path/to/credentials.json ? And how can we download it?
Taha
Firstly thanks for share this kind of code, after long r&d i got this, i am stuck in to get credentials path, where is credentials path in google my business api, because all code is set, i got secret key for authentication, but here is some error : ” Fatal error: Uncaught Google\Service\Exception: { “error”: { “code”: 401, “message”: “Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.”, “errors”: [ { “message”: “Login Required.”, “domain”: “global”, “reason”: “required”, “location”: “Authorization”, “locationType”: “header” } ], “status”: “UNAUTHENTICATED” } } in C:\xampp\htdocs\img\vendor\google\apiclient\src\Http\REST.php:128 Stack trace: #0 C:\xampp\htdocs\img\vendor\google\apiclient\src\Http\REST.php(103): Google\Http\REST::decodeHttpResponse(Object(GuzzleHttp\Psr7\Response), Object(GuzzleHttp\Psr7\Request), ‘Google_Service_…’) #1 [internal function]: Google\Http\REST::doExecute(Object(GuzzleHttp\Client), Object(GuzzleHttp\Psr7\Request), ‘Google_Service_…’) #2 C:\xampp\htdocs\img\vendor\google\apiclient\src\Task\Runner.php in C:\xampp\htdocs\img\vendor\google\apiclient\src\Http\REST.php on line 128
ok, hope you understand
vishal Hari
file “http://app.reviewwell.net/public/client_secret_858071815891-3u8t811n1bmh6n2lkv0jib6sfibthhnh.apps.googleusercontent.com.json” does not exist
when i implemented the library shows error
chirayu
SECRET_PATH and CREDENTIAL_PATH content difference ? how these two are different?