Here's an example PHP code for fetching Google Business reviews using the Google Places API
Mark E.
<?php
// Replace YOUR_API_KEY with your actual API key
$api_key = 'YOUR_API_KEY';
$place_id = 'YOUR_PLACE_ID'; // Replace with your actual place ID
$url = "https://maps.googleapis.com/maps/api/place/details/json?place_id=$place_id&fields=name,rating,reviews&key=$api_key";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json_data = curl_exec($ch);
curl_close($ch);
$data = json_decode($json_data, true);
if ($data['status'] == 'OK') {
$name = $data['result']['name'];
$rating = $data['result']['rating'];
$reviews = $data['result']['reviews'];
echo "<h2>$name</h2>";
echo "<p>Rating: $rating</p>";
foreach ($reviews as $review) {
$author_name = $review['author_name'];
$rating = $review['rating'];
$text = $review['text'];
$time = date('Y-m-d H:i:s', $review['time']);
echo "<p>Author: $author_name</p>";
echo "<p>Rating: $rating</p>";
echo "<p>Review: $text</p>";
echo "<p>Time: $time</p>";
echo "<hr>";
}
} else {
echo "Error: " . $data['status'];
}
?>
This code uses the curl
function to send a request to the Google Places API and retrieve the details for a specific place, including the name, rating, and reviews. The response data is then parsed as JSON and displayed in an HTML format.
Note that in order to use this code, you will need to obtain an API key from the Google Cloud Console and replace YOUR_API_KEY
and YOUR_PLACE_ID
with your actual values. Also, be sure to comply with the Google Places API usage limits and policies.