1

I'm trying to get reviews in Google Business. The goal is to get access via curl and then get value from pane.rating.moreReviews label jsaction.

How I can fix code below to get curl?

function curl($url) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36');
  $html = curl_exec($ch);
  curl_close($ch);
  return $html;
}

$html = curl("https://www.google.com/maps?cid=12909283986953620003");
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$finder = new DomXPath($DOM);
$classname = 'pane.rating.moreReviews';
$nodes = $finder->query("//*[contains(@jsaction, '$classname')]");
foreach ($nodes as $node) {
  $check_reviews = $node->nodeValue;
  $ses_key = preg_replace('/[^0-9]+/', '', $check_reviews);
}

// result should be: 166
echo $ses_key;

If I try do var_dump($html);, I'm getting:

string(348437) " "

And this number is changing on each page refresh.

Konrad Strek
  • 71
  • 1
  • 9

2 Answers2

5

Get Google-Reviews with PHP cURL & without API Key

How to find the CID - If you have the business open in Google Maps:

  • Do a search in Google Maps for the business name
  • Make sure it’s the only result that shows up.
  • Replace http:// with view-source: in the URL
  • Click CTRL+F and search the source code for “ludocid”
  • CID will be the numbers after “ludocid\u003d” and till the last number

or use this tool: https://ryanbradley.com/tools/google-cid-finder/

Example

ludocid\\u003d16726544242868601925\

HINT: Use the class ".quote" in you CSS to style the output

The PHP

<?php
/*

 Get Google-Reviews with PHP cURL & without API Key
=====================================================

How to find the CID - If you have the business open in Google Maps:
- Do a search in Google Maps for the business name
- Make sure it’s the only result that shows up.
- Replace http:// with view-source: in the URL
- Click CTRL+F and search the source code for “ludocid”
- CID will be the numbers after “ludocid\\u003d” and till the last number

or use this tool: https://pleper.com/index.php?do=tools&sdo=cid_converter

Example
-------
```TXT
ludocid\\u003d16726544242868601925\
```

> HINT: Use the class ".quote" in you CSS to style the output

###### Copyright 2019 Igor Gaffling

*/

$cid = '16726544242868601925';   // The CID you want to see the reviews for
$show_only_if_with_text = false; // true OR false
$show_only_if_greater_x = 0;     // 0-4
$show_rule_after_review = false; // true OR false
$show_blank_star_till_5 = true;  // true OR false
/* ------------------------------------------------------------------------- */

$ch = curl_init('https://www.google.com/maps?cid='.$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');
$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[0][1][0][14][18];
  //$reviews = $reviews[0][1][0][14][52][0];
  $customer = $reviews[6][18]; // NEW IN 2020
  $reviews  = $reviews[6][52][0]; // NEW IN 2020
}
if (isset($reviews)) {
  echo '<div class="quote"><strong>'.$customer.'</strong><br>';
  foreach ($reviews as $review) {
    if ($show_only_if_with_text == true and empty($review[3])) continue;
    if ($review[4] <= $show_only_if_greater_x) continue;
    for ($i=1; $i <= $review[4]; ++$i) echo '⭐'; // RATING
    if ($show_blank_star_till_5 == true)
      for ($i=1; $i <= 5-$review[4]; ++$i) echo '☆'; // RATING
    echo '<p>'.$review[3].'<br>'; // TEXT
    echo '<small>'.$review[0][1].'</small></p>'; // AUTHOR
    if ($show_rule_after_review == true) echo '<hr size="1">';
  }
  echo '</div>';
}

Source: https://github.com/gaffling/PHP-Grab-Google-Reviews

adilbo
  • 910
  • 14
  • 22
2

Please try below code

$html = curl("https://maps.googleapis.com/maps/api/place/details/json?cid=12909283986953620003&key=<google_apis_key>", "Mozilla 5.0");
$datareview = json_decode($html);// get all data in array

Ex. : http://meetingwords.com/QiIN1vaIuY

It will work for you.

Create Google Key From google console developer

https://developers.google.com/maps/documentation/embed/get-api-key

Praveen Kumar
  • 864
  • 2
  • 9
  • 33
  • I know, but this is not related to my question. I will not count number of all reviews using this. – Konrad Strek Oct 23 '18 at 09:58
  • @Konrad :- Please try this code you will get all data (review) in JSON format. I check and I get all review http://meetingwords.com/dNQ7QNDaih – Praveen Kumar Oct 23 '18 at 10:06
  • @Konrad :- please check updated answered and ex . http://meetingwords.com/QiIN1vaIuY – Praveen Kumar Oct 23 '18 at 10:27
  • Also with an API call, you would not get all reviews from google – adilbo May 18 '21 at 07:44
  • @PraveenKumar seems like your URL http:://meetingwords.com/QiIN1vaIuY not working as it keeps loading. I have couple of more questions. Can you please guide me here https://stackoverflow.com/questions/68862167/google-maps-place-get-average-rating-php-api – Mittul At TechnoBrave Aug 20 '21 at 12:34