douxia6163 2014-02-21 12:40
浏览 78
已采纳

curl返回null值

I am working on Geo coding some addresses and I found an example that uses cURL to extract the json data. For some reason it keeps returning null I have echoed the $url and copied it into the address field of my browser and it works as intended so I know the URL is valid. Does anyone spot a mistake in my code?

for($count=0; $count < count($uploadedArray); $count++) {
    $address = "";
    $address .= $uploadedArray[$count]["Address"] . " ";
    $address .= $uploadedArray[$count]["City"] . ", ";
    $address .= $uploadedArray[$count]["State"] . " ";
    $address .= $uploadedArray[$count]["Zip"];
    $url = urlencode("https://maps.googleapis.com/maps/api/geocode/json?address=" . $address . "=%s&sensor=false");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $geoloc = json_decode(curl_exec($ch), true);
    var_dump($geoloc);
    echo $geoloc['results'][0]['geometry']['location']['lat']; // get lat for json
    echo $geoloc['results'][0]['geometry']['location']['lng']; // get ing for json
}
  • 写回答

3条回答 默认 最新

  • doushi5752 2014-02-21 12:47
    关注

    You shouldn't use urlencode on the whole url. You should use it only on the parameters:

    $url = "https://maps.googleapis.com/maps/api/geocode/json?address=" . urlencode($address) . "=%s&sensor=false";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 WSL上下载的joern在windows怎么用?
  • ¥17 MC9S12XS128单片机开发板实验
  • ¥15 jetson nano4GB
  • ¥15 电脑回复出厂设置,重装过程报错提示,求解决方案Windows 无法分析或处理无人参与应答文件 [C:\WINDOWS\Panther\unattend.xml,如何解决?
  • ¥15 进入lighttools中的UDOP编辑器的方法
  • ¥15 求Gen6d训练数据集
  • ¥20 liunx中winscp中可以登入ftp,但是不能登入sftp,如何解决
  • ¥15 lighttools的光学属性自定义的用法流程
  • ¥15 uni-app动态修改推荐页标题时报错
  • ¥15 图片是GET请求,后面是图片的id,Glide如何缓存?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部