file_get_contents返回空字符串[duplicate]

问题描述 投票:0回答:1

这个问题在这里已有答案:

运行以下代码时:

  <?php
$html = file_get_contents('https://www.eltenedor.es/');

$albergina_doc = new DOMDocument();

libxml_use_internal_errors(TRUE);
if(!empty($html)){
  $albergina_doc->loadHTML($html);
  libxml_clear_errors();

  $albergina_xpath = new DOMXPath($albergina_doc);
  $customer = array();
  $review = array();
  $albergina_array = array();
  $albergina_review = $albergina_xpath->query('//div[@class="reviewItem-customerComment"]'); //scrapping basico
    $albergina_customer = $albergina_xpath->query('//div[@class="reviewItem-profileInfo"]'); //scrapping basico

foreach ($albergina_customer as $row) {
  $content = explode(' ', $row->nodeValue);
  $customer_name = $content[40]." ".$content[41];
  array_push($customer,$customer_name);

}

foreach ($albergina_review as $row) {
    array_push($review,$row->nodeValue);
}

for($i = 0; $i<count($customer); $i++){
  array_push($albergina_array, array('customer' => $customer[$i] , 'review' => $review[$i]));
}


echo (json_encode($albergina_array));


}

我在本地服务器上获得$ html的结果是肯定的。但是,当我将其上传到我的FTP并运行它时,$ html的内容为空并且不会引发任何错误。有人知道会出现什么问题吗?

php web
1个回答
2
投票

看看错误日志 - 如果你使用file_get_contents作为https网址,可能会有丢失包装的错误。

How to get file_get_contents() to work with HTTPS?包含以下代码来检查已注册的包装器:

$w = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_export($w);
© www.soinside.com 2019 - 2024. All rights reserved.