php - 简单的 html dom 解析器 $html 为空

标签 php simple-html-dom

当尝试使用简单的 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/

相关文章:

php - 在页面刷新时保持按钮状态?

php - 简单的 DOM file_get_html 不返回任何内容

php - 从 php 数组中回显一个项目

php - 简单的 html dom 查找未指定的元素

javascript - Owl Carousel 未在 Elementor 实时预览中显示

php - 使用 ini_set ("memory_limit", "-1")并且仍然内存不足

php - 使用 PHPQuery 的问题

PHP 简单 HTML DOM 解析器查找直接 LI 元素

php - 为什么在 PHP 中使用 "lock"文件而不是仅仅计算进程数?

php mysql 表单不插入数据