admin管理员组

文章数量:1359218

I know this question is mon, 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 pany wtih a group of locations fully verified by Google. () My only one Goal for now is to list all Google Reviews, and display them on the website of the pany. 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} (/{accountId}/locations/{locationId}/reviews/) I managed to fetch all the reviews by adding access token - (/{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 pany´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 ()

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

I know this question is mon, 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 pany wtih a group of locations fully verified by Google. (https://developers.google./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 pany. 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./v4/accounts/{accountId}/locations/{locationId}/reviews/) I managed to fetch all the reviews by adding access token - (https://mybusiness.googleapis./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 pany´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./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

Share Improve this question asked Apr 6, 2022 at 16:16 ITS Tecnologia CriativaITS Tecnologia Criativa 211 gold badge1 silver badge4 bronze badges 2
  • Its the same as me, went through Google PHP Client Libraries and all my tests did not work. – Kaloy Commented Apr 29, 2022 at 14:52
  • For sure Bro! The Google Team are changing lot of its politics and there´s many API that are deprecated. I´m wating for this changes to bee stable to implement my code. The Google account that my customer use on GCP Console is the basic one, and the support is so poor that the were limited to say to me that they´re ain´t has a solution to my needs... So, just wait and see... – ITS Tecnologia Criativa Commented May 1, 2022 at 18:11
Add a ment  | 

3 Answers 3

Reset to default 4

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./gmb/?placeid=ChIJCezbnsal2YgRySx0GXk5hEo

Typically using an API involves,

  1. Fetching data from a URL
  2. 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./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./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.

Hope it isn't too late for the answer.

One approach is to store the clientId, clientSecret, and refreshToken generated from the Auth2.0 playground on your server. Periodically refresh the accessToken (POST https://www.googleapis./oauth2/v4/token) when it expires.

Afaik, Google doesn't permit the use of service accounts for GMB, so this seems to be the only way.

P.S. You'll need to address scenarios where your Auth creds bee invalid. (Should not happen ideally)

本文标签: javascriptHow to display Google Locations reviews on my website with google apiStack Overflow