【问题标题】:Simple PHP web crawler returning simple HTML DOM error简单的 PHP 网络爬虫返回简单的 HTML DOM 错误
【发布时间】:2013-09-27 16:47:51
【问题描述】:

我有一个返回网页链接的 PHP 脚本。我收到 500 个内部错误,这就是我的服务器日志所说的。我让我的朋友在他的服务器上尝试相同的代码,它似乎运行正确。有人可以帮我调试我的问题吗?警告说有关包装器的某些内容已禁用。我检查了第 1081 行,但没有看到 allow_url_fopen

PHP 警告:file_get_contents(): http:// wrapper 在服务器配置中被 /hermes/bosweb/web066/b669/ipg.streamversetv/simple_html_dom.php 第 1081 行中的 allow_url_fopen=0 禁用

PHP 警告:file_get_contents(http://www.dota2lounge.com/):未能打开流:在第 1081 行的 /hermes/bosweb/web066/b669/ipg.streamversetv/simple_html_dom.php 中找不到合适的包装器

PHP 致命错误:在 /hermes/bosweb/web066/b669/ipg.streamversetv/sim 中的非对象上调用成员函数 find()

<?php
 include_once('simple_html_dom.php');
 $target_url = 'http://www.dota2lounge.com/';
 $html = new simple_html_dom();
 $html->load_file($target_url);
  foreach($html->find(a) as $link){
    echo $link->href.'<br />';
  }
?>

【问题讨论】:

  • 服务器禁用了将file_get_contents() 用于远程文件的选项。
  • 错误信息非常清楚。第 1081 行是您尝试在 fopen 上下文中使用 url 的地方。您需要查看您的 php.ini 以启用该设置。
  • 在哪里可以找到我的 php.ini?
  • allow_url_fopen 是 php.ini 中的设置参数
  • 没关系,我找到了。感谢您的帮助!

标签: php web-crawler


【解决方案1】:
  1. 下载最新的simple_html_dom.php:LINK TO DOWNLOAD

  2. 在您喜欢的编辑器中打开 simple_html_dom.php 并将这段代码添加到第一行(可以在&lt;?php 之后添加):

    function file_get_contents_curl($url) {
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);     
    
        $data = curl_exec($ch);
        curl_close($ch);
    
        return $data; }
    
  3. 查找以function file_get_html($url..... 开头的行对我来说是第 71 行,但您也可以在编辑器中使用搜索。 (搜索 file_get_html)

  4. 编辑这一行(函数file_get_html之后的一些行):

    $contents = file_get_contents($url, $use_include_path, $context, $offset);

    到这里:

    $contents = file_get_contents_curl($url);

  5. 使用 file_get_html 代替 load_file,它会为您工作,无需编辑 php.ini

【讨论】:

  • 请澄清第五点。在哪里使用“file_get_html”而不是“load_file”? @DB5
  • @SikandarAliChishty 我只编辑了这个答案以使格式更清晰。 p.tamas 是写答案的人,您需要将问题直接问他。
  • 请澄清第五点。在哪里使用“file_get_html”而不是“load_file”? @p.tamas
  • 我在日志中遇到了同样的错误,更改为 curl 版本现在可以正常工作了。
  • 我希望我能更多地支持这个,非常好的解决方案
【解决方案2】:

您需要将allow_url_fopen php 设置为 1 以允许将fopen() 与 url 一起使用。

参考:PHP: Runtime Configuration

编辑:
还追踪到了另外一个东西,你试过这样加载吗?

<?php
    include_once('simple_html_dom.php');

    $html = file_get_html('http://www.dota2lounge.com/');

    foreach($html->find('a') as $link)
    {
        echo $link->href.'<br />';
    }
?>

【讨论】:

  • 我刚刚改了,allow_url_fopen = 1。仍然报500错误,我应该给我的服务器一些时间来改吗?
  • 现在测试它会发生什么?如果您仍然收到相同的消息,可能是因为您需要在 php.ini 设置中更改它并重新启动服务器。
  • 我仍然收到 500 错误,我不确定实际错误是什么,因为我的日志尚未更新。我将如何去重置服务器?我的网站托管在 ipage.com
  • 您可以尝试将此行添加到您的 .htaccess 中:php_value allow_url_fopen On 它可能不起作用,具体取决于您的服务器配置方式。顺便说一句,500 是服务器错误,所以如果您尝试连接到其他人的站点,他们的 .htaccess 可能会阻止您访问该文件。
  • 这是我得到的新错误。 20130927T131614:stre/crawler.php PHP 致命错误:在第 1113 行的 ipg.htfhft/simple_html_dom.php 中的非对象上调用成员函数 find()
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 2017-01-26
  • 2016-06-23
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 2015-06-26
  • 2016-10-31
相关资源
最近更新 更多