php file_get_contents($ url)没有返回与地址栏中的url相同的结果

时间:2016-12-31 15:56:08

标签: php file-get-contents

当我将以下网址复制并粘贴到地址栏中时,它会正确打开页面:

https://www.lacourt.org/casesummary/ui/casesummary.aspx?CaseNumber=BC510457

但是当我在localhost上运行时,以下代码会从站点返回一个case not found消息:

    <?php
    $url = 'https://www.lacourt.org/casesummary/ui/casesummary.aspx?CaseNumber=BC510457';
    echo file_get_contents($url);
?>

为什么file_get_contents没有返回与我直接在地址栏中输入网址时相同的页面?有什么建议吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

首先,我无法连接网址, 但您可以尝试在请求标头中设置user-agent。 像这样:)

`

$url = 'https://www.lacourt.org/casesummary/ui/casesummary.aspx?CaseNumber=BC510457';
$header = [
'header'=>[
'method'=>'GET',
'header'=>['user-agent'=>'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36']
]
];
$ctx = stream_context_create($header);
echo file_get_contents($url,false,$ctx);

`