使用Namingm和php文件\u get\u contents()或Curl无法从openstreet map获取任何json结果
我正在尝试从openstreetmap.org获取的json文件中获取结果。在浏览器中输入url时,我看到json文件在浏览器中返回。如果我尝试使用php脚本读取json,那么什么都不会发生。如果我使用file\u get\u contents,也不会使用curl使用Namingm和php文件\u get\u contents()或Curl无法从openstreet map获取任何json结果,json,openstreetmap,php-7.4,Json,Openstreetmap,Php 7.4,我正在尝试从openstreetmap.org获取的json文件中获取结果。在浏览器中输入url时,我看到json文件在浏览器中返回。如果我尝试使用php脚本读取json,那么什么都不会发生。如果我使用file\u get\u contents,也不会使用curl function geocode($address){ // url encode the address $address = urlencode($address); //Url openstreetm
function geocode($address){
// url encode the address
$address = urlencode($address);
//Url openstreetmap
$url = "https://nominatim.openstreetmap.org/?addressdetails=1&q=$address&format=json&limit=1";
// Initiate curl
$ch = curl_init();
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
return json_decode($result, true);
}
而且,如果我使用file_get_内容,则不会有结果:
function geocode($address){
// url encode the address
$address = urlencode($address);
$url = "http://nominatim.openstreetmap.org/search/?format=json&addressdetails=1&q={$address}&format=json&limit=1";
// get the json response
$resp_json = file_get_contents($url);
return json_decode($resp_json, true);
}
我可能做错了什么?我已根据您的要求更改了您的代码 简而言之,CURL是一种更好的方法,但必须至少添加以下HTTP请求头:
- HTTP引用程序
- 用户代理
<?php
function geocode($address){
$address = urlencode($address);
$url = "https://nominatim.openstreetmap.org/?addressdetails=1&q=$address&format=json&limit=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
echo "<pre>";
print_r(geocode("Time Square, New York City"));
啊,我现在明白了,它的工作非常出色!
Array
(
[0] => Array
(
[place_id] => 162597874
[licence] => Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright
[osm_type] => way
[osm_id] => 304980452
[boundingbox] => Array
(
[0] => 41.7382344
[1] => 41.7384706
[2] => -74.0371548
[3] => -74.0367398
)
[lat] => 41.73835325
[lon] => -74.03694730280651
[display_name] => Time Square, 652, NY 299, Elting Corners, Lloyd, Town of Lloyd, Ulster County, New York, 12561, United States
[class] => building
[type] => yes
[importance] => 0.401
[address] => Array
(
[building] => Time Square
[house_number] => 652
[road] => NY 299
[hamlet] => Elting Corners
[town] => Lloyd
[municipality] => Town of Lloyd
[county] => Ulster County
[state] => New York
[postcode] => 12561
[country] => United States
[country_code] => us
)
)
)