Skip to content
Advertisement

cURL issue with Google consent redirect

I’m running into an issue with cURL while getting customer review data from Google (without API). Before my cURL request was working just fine, but it seems Google now redirects all requests to a cookie consent page.

Below you’ll find my current code:

$ch = curl_init('https://www.google.com/maps?cid=4493464801819550785');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

print_r($result);

$result now just prints “302 Moved. The document had moved here.”

I also tried setting curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); but that didn’t help either.

Does anyone has an idea on how to overcome this? Can I programmatically deny (or accept) Google’s cookies somehow? Or maybe there is a better way of handling this?

Advertisement

Answer

What you need is the following:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

The above curl option is what tells curl to follow redirects. However, I am not sure whether what is returned will be of much use for the specific URL you are trying to fetch. By adding the above option you will obtain the HTML source for the final page Google redirects to. But this page contains scripts that when executed load the map and other content that is ultimately displayed in your browser. So if you need to fetch data from what is subsequently loaded by JavaScript, then you will not find it in the returned results. Instead you should look into using a tool like selenium with PHP (you might take a look at this post).

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement