我想获取url的内容,当我使用file_get_contents方法时,它返回HTTP / 1.1 301 Moved Permanently。但在浏览器中链接工作正常。
我也试过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]);
提前致谢!
答案 0 :(得分:1)
更改以下内容,因为curl应该能够遵循重定向
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
答案 1 :(得分:0)
试试这个
$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);
答案 2 :(得分:-1)
谢谢你们所有人。我找到了问题的答案。
$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);