Skip to content

Instantly share code, notes, and snippets.

@shramee
Last active March 11, 2024 18:34
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shramee/fc115a909fd1f8726a589d1b0b351e3e to your computer and use it in GitHub Desktop.
Save shramee/fc115a909fd1f8726a589d1b0b351e3e to your computer and use it in GitHub Desktop.
Get and shows google reviews
<?php
/**
* 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');
}
// Get reviews
$g_response = get_google_reviews();
// 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
}
}
?>
<!-- OPTIONAL: Some styles to make things look nicer -->
<style>
dl {
display: flex;
flex-wrap: wrap;
font: 1rem/1.4 sans-serif;
max-width: 500px;
margin: 2em auto 3em;
}
dt, dd {
margin: .5em 0;
}
dt {
flex: 1 1 25%;
}
dd {
flex: 1 1 75%;
}
</style>
@Amirul5
Copy link

Amirul5 commented Sep 15, 2022

gid:AHaTB7Z93wu3gnQXmrpEG5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment