11
<?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

4 回答 4

39

正如我所说,您的示例对我来说效果很好......但是请尝试使用 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...

于 2013-09-07T00:45:03.020 回答
3

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

; Windows Extensions
...
;extension=php_openssl.dll
...
于 2016-08-23T02:05:09.243 回答
2

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

于 2015-01-07T23:01:25.700 回答
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: 放错了这是 URL: http://www.annauniv.edu/cgi-bin/result/cgrade.pl?regno=11210104001

于 2014-03-31T08:18:00.357 回答