Friday, October 28, 2022

[SOLVED] How to get reviews from Google Business using CURL PHP

Issue

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.


Solution

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
/* ------------------------------------------------------------------------- */

$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



Answered By - adilbo
Answer Checked By - Mildred Charles (WPSolving Admin)