简单的html dom file_get_html无法正常工作 - 是否有任何解决方法?

时间:2013-09-06 22:20:50

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

<?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功能来阻止它?

以上代码适用于其他网站。

有解决方法吗?

4 个答案:

答案 0 :(得分:31)

正如我所说,你的例子对我来说很好......但是尝试使用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);

它按预期获得所有链接:

enter image description here

并确保您安装了php_opensslphp_curl ...

答案 1 :(得分:3)

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

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

答案 2 :(得分:2)

你必须设置&#34; allow_url_fopen&#34;在&#34; php.ini&#34;中为TRUE允许通过HTTP或FTP访问文件 一些主机供应商禁用了PHP&#34; allow_url_fopen&#34;安全问题的标志。

答案 3 :(得分:1)

$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:misplaced 这是网址:http://www.annauniv.edu/cgi-bin/result/cgrade.pl?regno=11210104001