href ); echo ""; } -6ren">
gpt4 book ai didi

php - 简单的 html dom file_get_html 不工作 - 有什么解决方法吗?

转载 作者:可可西里 更新时间:2023-10-31 22:53:23 30 4
gpt4 key购买 nike

<?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 函数来阻止它?

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

有什么解决方法吗?

最佳答案

正如我所说,您的示例对我来说工作正常......但请尝试使用 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...

关于php - 简单的 html dom file_get_html 不工作 - 有什么解决方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18667441/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com