PHP 卷曲/文件获取内容不起作用


PHP Curl / File Get Contents not working

我正在尝试执行以下操作

$url = "http://halo-video.com/drama/ha";
$data = file_get_contents($url);
echo $data;

它给了我一个空白页或 500 内部错误,我尝试用我的计算机加载该网站,它的加载很好。

虽然原因可能不同,但我的代码(类似于你的)工作正常,所以只需将这些行附加到你的适当位置,

$useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; nl; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7";
                 curl_setopt($s,CURLOPT_AUTOREFERER, true);
                    curl_setopt($s,CURLOPT_FOLLOWLOCATION, true);
                    curl_setopt($s,CURLOPT_MAXREDIRS, 5);
                    curl_setopt($s,CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($s, CURLOPT_HEADER, true);

编辑:

好的,这困扰着我,所以想出了一个解决方法。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://kissanime.com/Anime/Karneval');
curl_setopt($ch, CURLOPT_USERAGENT, 'myuseragent'); 
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_REFERER, "http://www.test.com");
$html = curl_exec($ch);
curl_close($ch);
echo $html;

这段代码有效并修复了我遇到的重定向错误。 快乐的内容抓取。

使用 file() 代替 file_get_contents()

$url = "http://kissanime.com/Anime/Karneval";
$data = file($url);
echo htmlspecialchars($data);