PHP file_get_contents 在 OpenStreetMap API 和 returns 中使用 & 而不是 & 403
PHP file_get_contents uses & instead of & in the OpenStreetMap API and returns a 403
我使用 OpenStreetMap API 返回 XML。所以基本上使用 Search for Test (与 ?q=test&format=xml
)例如在英格兰找到一条河流。到目前为止一切顺利。
我的问题是 API 非常严格。所以搜索 Search for Test(使用 q=test&format=xml
)是(当使用普通浏览器时)重定向到上面的 url。但是,如果您 复制粘贴 URL 和 ?q=test&format=xml
它应该如下所示:
这不是 XML。所以 URL 不符合 OpenStreetMap。因为现在无法识别 format
参数。
这只是我的问题。所以当我尝试在 PHP 中使用时: file_get_contents("http://nominatim.openstreetmap.org/search?q=test&format=xml");
我得到异常:
Exception has occurred. Warning:
file_get_contents(http://nominatim.openstreetmap.org/search?q=test&format=xml):
failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
我不完全确定是什么导致了这个 &
重写。是 OpenStreetMap 还是 PHP file_get_contents
?我已经看过以下问题。但遗憾的是 none 这些解决方案确实帮助了我:
- https://webmasters.stackexchange.com/questions/52822/api-demands-and-not-amp
- (我无法简单地忽略错误的解决方案)
- php file_get_contents and &
还尝试使用 php.ini 值:
- arg_separator.output = "&"
- arg_separator.input = ";&"
实际上不是 &
的问题。通过修复 403 错误 它可以正常工作。 帮助我找到了解决方案,因为 OpenStreetMap 现在需要一个 http referer。
所以这段代码解决了我的问题:
$referer = "https://nominatim.openstreetmap.org/search?q=test&format=xml";
$opts = array(
'http'=>array(
'header'=>array("Referer: $referer\r\n")
)
);
$context = stream_context_create($opts);
$myURL = file_get_contents($referer, false, $context);
$referer = "https://nominatim.openstreetmap.org/search?q=test&format=xml";
请将XML更改为JSON,然后从API服务器获取数据。
我使用 OpenStreetMap API 返回 XML。所以基本上使用 Search for Test (与 ?q=test&format=xml
)例如在英格兰找到一条河流。到目前为止一切顺利。
我的问题是 API 非常严格。所以搜索 Search for Test(使用 q=test&format=xml
)是(当使用普通浏览器时)重定向到上面的 url。但是,如果您 复制粘贴 URL 和 ?q=test&format=xml
它应该如下所示:
这不是 XML。所以 URL 不符合 OpenStreetMap。因为现在无法识别 format
参数。
这只是我的问题。所以当我尝试在 PHP 中使用时: file_get_contents("http://nominatim.openstreetmap.org/search?q=test&format=xml");
我得到异常:
Exception has occurred. Warning: file_get_contents(http://nominatim.openstreetmap.org/search?q=test&format=xml): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
我不完全确定是什么导致了这个 &
重写。是 OpenStreetMap 还是 PHP file_get_contents
?我已经看过以下问题。但遗憾的是 none 这些解决方案确实帮助了我:
- https://webmasters.stackexchange.com/questions/52822/api-demands-and-not-amp
- (我无法简单地忽略错误的解决方案)
- php file_get_contents and &
还尝试使用 php.ini 值:
- arg_separator.output = "&"
- arg_separator.input = ";&"
实际上不是 &
的问题。通过修复 403 错误 它可以正常工作。
所以这段代码解决了我的问题:
$referer = "https://nominatim.openstreetmap.org/search?q=test&format=xml";
$opts = array(
'http'=>array(
'header'=>array("Referer: $referer\r\n")
)
);
$context = stream_context_create($opts);
$myURL = file_get_contents($referer, false, $context);
$referer = "https://nominatim.openstreetmap.org/search?q=test&format=xml";
请将XML更改为JSON,然后从API服务器获取数据。