【问题标题】:Simple html dom file_get_html not working - is there any workaround?简单的 html dom file_get_html 不起作用 - 有什么解决方法吗?
【发布时间】:2013-09-11 02:59:17
【问题描述】:
<?php
// Report all PHP errors (see changelog)
error_reporting(E_ALL);

include('inc/simple_html_dom.php');

    //base url
    $base = 'https://play.google.com/store/apps';

    //home page HTML
    $html_base = file_get_html( $base );

    //get all category links
    foreach($html_base->find('a') as $element) {
        echo "<pre>";
        print_r( $element->href );
        echo "</pre>";
    }

    $html_base->clear(); 
    unset($html_base);

?>

我有上面的代码,我正在尝试获取 Play 商店页面的某些元素,但它没有返回任何内容。是否有可能在服务器上禁用某些 PHP 功能来阻止它?

以上代码在其他网站上也能完美运行。

有什么解决办法吗?

【问题讨论】:

  • 对我来说工作得很好......

标签: php html-parsing file-get-contents simple-html-dom


【解决方案1】:

正如我所说,您的示例对我来说效果很好...但是请尝试使用 curl 代替:

//base url
$base = 'https://play.google.com/store/apps';

$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $base);
curl_setopt($curl, CURLOPT_REFERER, $base);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$str = curl_exec($curl);
curl_close($curl);

// Create a DOM object
$html_base = new simple_html_dom();
// Load HTML from a string
$html_base->load($str);

//get all category links
foreach($html_base->find('a') as $element) {
    echo "<pre>";
    print_r( $element->href );
    echo "</pre>";
}

$html_base->clear(); 
unset($html_base);

它按预期获取所有链接:

并确保您已安装 php_opensslphp_curl...

【讨论】:

  • 哇,谢谢你,正如你所说,我只需要激活“php_openssl”扩展,它现在可以工作了:) 我在 Windows 上使用 WAMP 服务器,默认情况下它是不活动的。谢谢大佬!
【解决方案2】:
$post = curl_init(); 
curl_setopt($post, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($post, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($post, CURLOPT_HEADER, 0);
curl_setopt($post,CURLOPT_RETURNTRANSFER, true);
curl_setopt($post,CURLOPT_URL,$website);
curl_setopt($post,CURLOPT_POST,1);
curl_setopt($post,CURLOPT_POSTFIELDS,"regno=$Number");
curl_setopt($post, CURLOPT_FOLLOWLOCATION, True);
curl_getinfo($post, CURLINFO_HTTP_CODE);
$curlresponse = curl_exec($post);
curl_close($post);  
$dom = new DOMDocument();
$dom->loadHTML($curlresponse);

DOMDocument::loadHTML() [domdocument.loadhtml]: htmlParseStartTag: 错位 这是网址:http://www.annauniv.edu/cgi-bin/result/cgrade.pl?regno=11210104001

【讨论】:

    【解决方案3】:

    您必须在“php.ini”中将“allow_url_fopen”设置为 TRUE,以允许通过 HTTP 或 FTP 访问文件。
    一些托管供应商出于安全问题禁用了 PHP 的“allow_url_fopen”标志。

    【讨论】:

      【解决方案4】:

      从 php.ini 中删除分号并重新启动 Apache 服务器以启用 php 模块配置

      ; Windows Extensions
      ...
      ;extension=php_openssl.dll
      ...
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-31
        • 1970-01-01
        • 2016-05-06
        • 1970-01-01
        相关资源
        最近更新 更多