URL使用&;但不是&;amp;


URL working with & but not &

我有这个链接,它运行良好。

http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?boro=3&houseno=512&street=bedford%20ave&requestid=0&s=A03C41B885B461E4F46BD08866A7430E

我想获取这个URL的内容,但问题是file_get_contents内容将&转换为&

http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?boro=3&houseno=512&street=bedford%20ave&requestid=0&s=A03C41B885B461E4F46BD08866A7430E

这不起作用。

$url = "http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?boro=3&houseno=512&street=bedford%20ave&requestid=0&s=A03C41B885B461E4F46BD08866A7430E";
$content = file_get_contents($url);
echo $content;

它会产生错误。

$url = "http://www.google.com";
$content = file_get_contents($url);
echo $content;

它运行良好。

在没有为请求设置User-Agent的情况下,我得到了一个403禁止的错误。

此代码有效:

<?php
$url = "http://a810-bisweb.nyc.gov/bisweb/PropertyProfileOverviewServlet?boro=3&houseno=512&street=bedford%20ave&requestid=0&s=A03C41B885B461E4F46BD08866A7430E";
$opts = array(
    'http' => array('header' => 'User-Agent: test'),
);
$ctx = stream_context_create($opts);
$content = file_get_contents($url, false, $ctx);
var_dump($content);

看起来网站默认情况下不允许PHP用户代理,所以您需要使用流上下文指定一个,或者使用另一个库(如cURL)来获取内容。

当尝试使用file_get_contents获取远程URL时,这是一个常见的问题。