Fetching Google Reviews Results with PHP

To fetch Google Reviews using PHP, you’ll need to make use of the Google Places API, specifically the Place Details request. This involves sending an HTTP request to Google’s API endpoint and parsing the JSON response to extract the reviews. Here’s a PHP code snippet that demonstrates this:

<?php
// Replace with your actual API key and Place ID
$apiKey = 'YOUR_API_KEY';
$placeId = 'YOUR_PLACE_ID';

// URL for the Google Places API request
$url = "https://maps.googleapis.com/maps/api/place/details/json?placeid=$placeId&fields=reviews&key=$apiKey";

// Fetch data from the API
$response = file_get_contents($url);

if ($response !== false) {
    $data = json_decode($response, true);

    if ($data['status'] === 'OK' && isset($data['result']['reviews'])) {
        $reviews = $data['result']['reviews'];

        // Process reviews if needed or store them in an array
        $formattedReviews = [];
        foreach ($reviews as $review) {
            $formattedReview = [
                'author_name' => $review['author_name'],
                'rating' => $review['rating'],
                'text' => $review['text'],
            ];
            $formattedReviews[] = $formattedReview;
        }

        // Store reviews in an array called 'reviews'
        $reviews = $formattedReviews;

        // Print or process the fetched reviews
        var_dump($reviews); // This will display the reviews as an array
    } else {
        echo 'No reviews found.';
    }
} else {
    echo 'Failed to fetch reviews.';
}
?>

Replace 'YOUR_API_KEY' with your actual Google API key and 'YOUR_PLACE_ID' with the Place ID of the desired location.

This code uses file_get_contents() to fetch data from the Google Places API endpoint. It then decodes the JSON response and extracts the reviews, storing them in an array called $reviews. You can further process or manipulate the reviews as needed within the PHP code. The var_dump($reviews) line is for demonstration purposes to display the fetched reviews in the PHP script.