简单的html dom file_get_html无法正常工作-是否有任何解决方法? [英] Simple html dom file_get_html not working - is there any workaround?

查看:215
本文介绍了简单的html dom file_get_html无法正常工作-是否有任何解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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功能以停止该功能?

I have the above code and I'm trying to get certain elements of the Play Store page but it isn't returning anything. Is it possible that certain PHP functions might be disabled on the server to stop that?

上面的代码在其他站点上也能很好地工作.

The above code works perfectly on other sites.

有什么解决方法吗?

推荐答案

正如我所说,您的示例对我来说很好...但是,可以使用curl尝试这种方式:

As I said, your example is working fine for me... But try this way using curl instead:

//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);

它将按预期获得所有链接:

It gets all the links as expected:

并确保已安装php_opensslphp_curl ...

And make sure you have php_openssl and php_curl installed...

这篇关于简单的html dom file_get_html无法正常工作-是否有任何解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆