简体   繁体   中英

Simple PHP Google Places API request

So if I put this in my browser URL:

https://maps.googleapis.com/maps/api/place/search/xml?location=45.508867,-73.554242&radius=500&sensor=false&key=mykey

It works. But if I try the exact same call in PHP I get ACCESS DENIED:

<?php
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/place/search/xml");
  curl_setopt($ch, CURLOPT_POSTFIELDS, "location=45.508867,-73.554242&radius=500&sensor=false&key=mykey");
  $result = curl_exec($ch);
  echo $result;
?> 

I tried many different options and minor changes but nothing works and I can't figure out why.

$latitude = 27.7172;
$longitude = 85.3240;
$radius = 50000;
$name = "kupondole";    
$url = "https://maps.googleapis.com/maps/api/place/search/json?name=".urlencode($name)."&location=".$latitude.",".$longitude."&radius=".$radius."&key=MY_KEY";
            $ch = curl_init();
           curl_setopt($ch, CURLOPT_URL, $url);
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
           curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
           curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
           curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            $response = curl_exec($ch);
          curl_close($ch);

Works perfectly fine.

如果出于测试目的而执行此操作,请确保未启用SSL验证,因为这将阻止它可能阻止请求表单的执行。

我相信您缺少此卷曲设置:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM