0

I am using google places API and when i request provided URL in chrome browser (on mac) than it returns more then 20 results.

But when i request same URL in PHP curl code it returns only 6 results.

Couldn't get it working. Please help.

            $ch = curl_init();
            $url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=31.4814662,74.411793&radius=20000&keyword='.urlencode('ac technician').'&key=API_KEY';
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            $response = curl_exec($ch);
            echo "#####".$response;            exit();
Gohar SUltan
  • 75
  • 1
  • 4
  • Does this help you? https://stackoverflow.com/questions/20785537/google-places-api-returning-only-5-results – kmoser May 08 '20 at 22:56
  • No, It doesn't. And i don't understand the overall API behaviour. because if simple request from browser is working so should CURL request. Is there any example code? – Gohar SUltan May 09 '20 at 11:27

1 Answers1

0

I couldn't reproduce this as your code is returning me 15 results for this same api call. I read a bit and it seems it returns only 5 results after doing many calls.

However, if you say that in a browser you get more results than in a curl call I would mimic the headers sent by the browser starting by the User-Agent. Check if adding this works for you:

$headers = array();
$headers[] = 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

This header is copying a header that could be sent by a firefox browser.

jeprubio
  • 17,312
  • 5
  • 45
  • 56