Php 文件获取内容返回永久移动的HTTP/1.1 301

Php 文件获取内容返回永久移动的HTTP/1.1 301,php,http,Php,Http,我想获取url的内容,当我使用file\u get\u contents方法时,它返回HTTP/1.1 301永久移动。但在浏览器中,链接可以正常工作 我也尝试了curl函数,但它返回了相同的问题。代码是 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $page_url); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE

我想获取url的内容,当我使用file\u get\u contents方法时,它返回HTTP/1.1 301永久移动。但在浏览器中,链接可以正常工作

我也尝试了curl函数,但它返回了相同的问题。代码是

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $page_url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
if(preg_match('#Location: (.*)#', $a, $r))
$xx = trim($r[1]);

提前谢谢

更改以下内容,因为curl应该能够跟踪重定向

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,TRUE);

试试这个

    $ch= curl_init();
    curl_setopt ($ch, CURLOPT_URL, $page_url );
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_VERBOSE,1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
    curl_setopt ($ch, CURLOPT_REFERER,'http://www.google.com');  //just a fake referer
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_POST,0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    $htmlContent= curl_exec($ch);
    curl_close($ch);

谢谢大家。我找到了我问题的答案

$opts = array('http'=>array('header' => "User-Agent:Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)\r\n"));
$context = stream_context_create($opts);
$buffer = file_get_contents($row['url'],false,$context);

谢谢你们的回复,我找到了答案,但我也尝试了你们的答案。它工作得很好。@KrishKowsi根据公认的答案,
file\u get\u contents()
不适用于所有网站。一些网站有
allow\u url\u fopen
disable。因此,您将无法从该网站获取内容<代码>卷曲库将完美工作。如果你需要更多的帮助,请告诉我。为什么这样做有效?提供用户代理标头是否会阻止站点重定向?