file_get_contents()不在具体网站上工作

时间:2014-09-03 00:25:27

标签: php html curl

我试图从网站www.pornhub.com获取HTML内容(不要问为什么:D)但经典

file_get_contents('http://www.pornhub.com');

不起作用。我也试过

$Url = 'https://www.pornhub.com/categories';

if (!function_exists('curl_init')){ 
    die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;

但它也不起作用。

有谁知道我做错了什么?

这是任何帮助。

1 个答案:

答案 0 :(得分:4)

请试试这个:

$Url = 'https://www.pornhub.com/categories';

if (!function_exists('curl_init')){
    die('CURL is not installed!');
}
$ch = curl_init($Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // add this one, it seems to spawn redirect 301 header
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); // spoof
$output = curl_exec($ch);
curl_close($ch);

echo $output; // use echo to show contents