Skip to content

Instantly share code, notes, and snippets.

@heyitsnovi
Created August 28, 2020 02:35
Show Gist options
  • Save heyitsnovi/d8d5e4d40abe97d28a75afcfbf46469d to your computer and use it in GitHub Desktop.
Save heyitsnovi/d8d5e4d40abe97d28a75afcfbf46469d to your computer and use it in GitHub Desktop.
<?php
/*
💬 Get Google-Reviews with PHP cURL & without API Key
=====================================================
**This is a dirty but usefull way to grab the first 8 most relevant reviews from Google with cURL and without the use of an API Key**
How to find the needed CID No:
- use: [https://pleper.com/index.php?do=tools&sdo=cid_converter]
- and do a search for your business name
Parameter
---------
```PHP
$options = array(
'google_maps_review_cid' => '17311646584374698221', // Customer Identification (CID)
'show_only_if_with_text' => false, // true = show only reviews that have text
'show_only_if_greater_x' => 0, // (0-4) only show reviews with more than x stars
'show_rule_after_review' => true, // false = don't show <hr> Tag after each review
'show_blank_star_till_5' => true, // false = don't show always 5 stars e.g. ⭐⭐⭐☆☆
'your_language_for_tran' => 'en', // give you language for auto translate reviews
);
echo getReviews($options);
```
> HINT: Use .quote in you CSS to style the output
###### Copyright 2019-2020 Igor Gaffling
*/
$options = array(
//'google_maps_review_cid' =>$_GET['cid'], // Customer Identification (CID)
'google_maps_review_cid'=>'',
'show_only_if_with_text' => false, // true = show only reviews that have text
'show_only_if_greater_x' => 0, // (0-4) only show reviews with more than x stars
'show_rule_after_review' => true, // false = don't show <hr> Tag after each review
'show_blank_star_till_5' => true, // false = don't show always 5 stars e.g. ⭐⭐⭐☆☆
'your_language_for_tran' => 'en', // give you language for auto translate reviews
);
/* ------------------------------------------------------------------------- */
/* ------------------------------------------------------------------------- */
function getReviews($option) {
$ch = curl_init('https://www.google.com/maps?cid='.$option['google_maps_review_cid']);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla / 5.0 (Windows; U; Windows NT 5.1; en - US; rv:1.8.1.6) Gecko / 20070725 Firefox / 2.0.0.6");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Language: '.$option['your_language_for_tran']));
$result = curl_exec($ch);
curl_close($ch);
$pattern = '/window\.APP_INITIALIZATION_STATE(.*);window\.APP_FLAGS=/ms';
if ( preg_match($pattern, $result, $match) ) {
$match[1] = trim($match[1], ' =;'); /* fix json */
$reviews = json_decode($match[1]);
$reviews = ltrim($reviews[3][6], ")]}'"); /* fix json */
$reviews = json_decode($reviews);
$customer = $reviews[6][11]; // NEW IN 2020
$reviews = $reviews[6][52][0]; // NEW IN 2020
}
$return = '';
if (isset($reviews)) {
$return .= '<div class="quote"><strong>'.$customer.'</strong><br>';
if ($option['show_rule_after_review'] == true) $return .= '<hr size="1">';
foreach ($reviews as $review) {
if ($option['show_only_if_with_text'] == true and empty($review[3])) continue;
if ($review[4] <= $option['show_only_if_greater_x']) continue;
for ($i=1; $i <= $review[4]; ++$i) $return .= '⭐'; /* RATING */
//months ago - var_dump($review[1]);
if ($option['show_blank_star_till_5'] == true)
for ($i=1; $i <= 5-$review[4]; ++$i) $return .= '☆'; /* RATING */
$return .= '<p>'.$review[3].'<br>'; /* TEXT */
//$review[9][1] biz reply
//echo($review[12][1][3]); image src
// stars integer echo $review[4];
$return .= '<img style="height:50px; width:50px;" src="'.$review[12][1][3].'"><small>'.$review[0][1].'</small></p>'; /* AUTHOR */
if ($option['show_rule_after_review'] == true) $return .= '<hr size="1">';
//echo($review[0][2]).'<br>';
//$review[12][1][3]
}
$return .= '</div>';
}
return $return;
}
function get_cid($url){
$ch = curl_init('https://www.google.com/maps/place/BIZZLOANS.COM.AU/@-33.8378548,151.2044583,17z/data=!3m2!4b1!5s0x6b12aef27e39146b:0xc4f8469e8839e27b!4m5!3m4!1s0x6b12aef27a700001:0xab503cf83b823e56!8m2!3d-33.8378548!4d151.206647');
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla / 5.0 (Windows; U; Windows NT 5.1; en - US; rv:1.8.1.6) Gecko / 20070725 Firefox / 2.0.0.6");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Language:en'));
$result = curl_exec($ch);
curl_close($ch);
$pattern = '/window\.APP_INITIALIZATION_STATE(.*);window\.APP_FLAGS=/ms';
$cid = get_string_between($result, 'u0026ludocid\\\\u003d', '#lrd');
return $cid;
}
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php var_dump(get_cid('')); // echo getReviews($options); ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment