当尝试使用简单的 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
执行相同操作时,我得到了正常响应。
我不太明白为什么,因为该网址工作正常。
$html
上的 var_dump
返回 bool (false)
。
我有 php 5.3.1,并且 php.ini 中的 allow_url_fopen
处于启用状态
最佳答案
使用cURL
并设置用户代理。显然,thepriatebay.se 不会在没有用户代理的情况下响应请求。
这会获取浏览器的用户代理并将其发送到目标。
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
要通过 cURL 请求网页,请使用以下命令:
// 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;
关于php - 简单的 html dom 解析器 $html 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15636072/