2013-03-26 125 views
2

当试图解析html时使用简单的html解析器,我没有得到任何回应。这里是代码:

$html = new simple_html_dom(); 
$html->file_get_html('http://thepiratebay.se/search/1080p/0/7/207'); 

$html什么也没有返回。但是,当我使用此网址做相同的事情时,http://thepiratebay.se/browse/207/0/7,我得到了正常的回应。

我真的不明白为什么,因为网址完美。

A var_dump on $html返回bool (false)

我有PHP 5.3.1和allow_url_fopen是在php.ini中

+0

我很抱歉,但我没有真正在这个问题笑...你得到任何形式的任何错误? – Seer 2013-03-26 12:18:45

回答

3

使用cURL,并设置一个用户代理。显然,没有用户代理的情况下,priatebay.se不会响应请求。

这会抓取浏览器的用户代理并将其发送到目标。

curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 

要通过卷曲请求网页,使用以下命令:

// Start a cURL resource 
$ch = curl_init(); 
// Set options for the cURL 
curl_setopt($ch, CURLOPT_URL, 'http://thepiratebay.se/search/1080p/0/7/207'); // target 
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // provide a user-agent 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow any redirects 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the result 
// Execute the cURL fetch 
$result = curl_exec($ch); 
// Close the resource 
curl_close($ch); 
// Output the results 
echo $result; 
+0

它的工作非常感谢! – user2211227 2013-03-26 13:17:38