简体   繁体   English

简单的PHP Google Places API请求

[英]Simple PHP Google Places API request

So if I put this in my browser URL: 因此,如果我将其放在浏览器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中尝试完全相同的调用,则会得到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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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