简体   繁体   中英

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 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

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

谷歌评论 API

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.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM