'How to display Google Locations reviews on my website with google api?
I know this question is common, however, I couldn't find any straight answers. All the answers have mentioned what is required to achieve this but no end solutions at all. Neither de G Docs are straightful on that. I´m trying to use the Google My Business API to get the reviews and ratings for a company wtih a group of locations fully verified by Google. (https://developers.google.com/my-business/content/review-data) My only one Goal for now is to list all Google Reviews, and display them on the website of the company. Make an API call (properly GET) via Ajax/PHP, or likely. API response with a list of google reviews (JSON format) Display the results in HTML.
- I have already followed the Prerequisites instruction step by step: Already have approval and test my API Creds with OAuth2 playground, and it's working fine. I managed to get the {accoundId} and {locationId} (https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/) I managed to fetch all the reviews by adding access token - (https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews/?access_token=token), But that's all been done through the OAuth2playground - The issue is I want to display the reviews on the website not by Google OAuth playground as it will get expired and reviews will not be updated automatically on the website.
I couldn't find any PHP / Javascript Code examples to start implementing this on company´s website. I was slightly confused about how to use the Google PHP Client Library for that. Too many ways, but none worked at my tests (https://github.com/googleapis/google-api-php-client)
If so, would this approach, will always prompt to ask the user to select which account to log in to? which is not the behavior I am looking for. I only want to get all the reviews and display them on the website, which should not require any login for the user.
It would be helpful if you could share the code and procedure.
Thanks in advance for any help. Best regards
Solution 1:[1]
Google lets you pull 5, it's been this way for a long, long time.
I wrote an API that interfaces with Google to do what you need, all you need is the Place ID and you can get the historic reviews, once you obtain historic reviews you can use the native API for real-time, don't hammer the API else it'll cap you :)
Hope it helps, see demo URL below (just swap the place ID with the ones you need and save what you consume to reviews.json and you have historic ratings).
https://api.reviewsmaker.com/gmb/?placeid=ChIJCezbnsal2YgRySx0GXk5hEo
Solution 2:[2]
Typically using an API involves,
- Fetching data from a URL
- Looping through/Using the data to do (output?) something
Example PHP implementation
Here's a PHP example implementation from one of my old projects. https://gist.github.com/shramee/fc115a909fd1f8726a589d1b0b351e3e
Fetching the reviews
We use cURL to fetch the reviews.
/**
* Get google reviews
* @return array Google reviews data
*/
function get_google_reviews(){
// URL to fetch
$google_api = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=<your_place_id>&sensor=true&key=<key>';
// Fetch reviews with cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $google_api);
$response = curl_exec($ch);
curl_close($ch);
// JSON decode the text to associative array
return json_decode($response, 'assoc');
}
Looping though the reviews
We then loop through the reviews and output some HTML,
// See if we got some reviews
if ($g_response && $g_response['result'] && $g_response['result']['reviews']) {
// Loop through the reviews
foreach ($g_response['result']['reviews'] as $review) {
// Output HTML for reviews
?>
<dl>
<dt> date </dt>
<dd><?php echo $review['time'] ?></dd>
<dt> rating </dt>
<dd><?php echo $review['rating'] ?></dd>
<dt> name </dt>
<dd><?php echo $review['author_name'] ?></dd>
<dt> content </dt>
<dd><?php echo $review['text'] ?></dd>
</dl>
<?php
}
}
For JavaScript, you'd use something like fetch/axios to get the data and then loop through the items to create some React elements or do some DOM manipulation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ilan P |
Solution 2 | shramee |