Get the number of reviews and rating for an object on Google map

Google Maps Google Places Get the number of reviews and rating for an object on Google map

Google Maps is a treasure trove of information, including valuable insights into businesses and points of interest through user reviews and ratings. This article provides a clear and simple guide on how to quickly find the number of reviews and the average rating for any location on Google Maps.

Get API key

Only authenticated API calls are allowed for Google Maps Platform products, ensuring protection against unauthorized use.

Go to credentials page, click on Create Credentials and select create API key.

https://console.cloud.google.com/apis/credentials

Get Google Place ID

Visit Find the ID of a particular place page and find your object on the map.

Find a place's ID using the place ID finder below.

https://developers.google.com/maps/documentation/places/web-service/place-id#places-api-new

Add credentials into .env

GOOGLE_PLACE_ID=[PLACE_ID]
GOOGLE_API_KEY=[API_KEY]
GOOGLE_REFERER=[SITE_ADDRESS(site.com)]

Set credentials in config/services.php

...
    'google_places' => [
        'place_id' => env('GOOGLE_PLACE_ID', ''),
        'api_key' => env('GOOGLE_API_KEY', ''),
        'referer' => env('GOOGLE_REFERER', ''),
    ],
...

Create Console for getting count reviews

<?php

namespace App\Console\Commands;

use App\Models\SocialCount;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;

class GoogleCountConsole extends Command
{
    /**
     * @var string
     */
    protected $signature = 'google:count';

    /**
     * @var string
     */
    protected $description = '';

    /**
     * @return void
     */
    public function handle(Cache $cache)
    {
        // Setting
        $googlePlaceId = config('services.google_places.place_id');
        $googleApiKey = config('services.google_places.api_key');
        $googleReferer = config('services.google_places.referer');

        // HTTP Client
        $response = Http::withHeaders([
            'Referer' => $googleReferer,
        ])
        ->get('https://places.googleapis.com/v1/places/' . $googlePlaceId, [
            'fields' => 'userRatingCount,rating',
            'key' => $googleApiKey,
        ]);

        //
        if ($response->successful()) {
            // Response
            $json = $response->json();

            /* Data */
	    echo "Rating Count: " . $json['userRatingCount'] . "\n";
            echo "Rating: " . $json['rating'] . "\n";            
        } else {
            echo $response->body() . "\n";
            logger()->error($response->body());
        }
    }
}

This PHP script defines a console command (google:count) that retrieves the number of user ratings and the average rating for a specific place on Google Maps using the Google Places API.

This command fetches the user rating count and average rating for a given Google Place ID using the Google Places API and displays the results in the console. It's a useful example of how to interact with external APIs within a Laravel application. Remember to configure your services.php file with the correct API key, Place ID, and referer.