1

When running the following code:

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


}

the result I get on $html in my local server is positive. However, when I upload it to my FTP and run it, the content of $html is empty and no error is thrown. Does anybody know what can be the problem?

Pablo Adell
  • 13
  • 1
  • 4

1 Answers1

2

Have a look at the error log - if you use file_get_contents for a https url, there might be errors thrown like missing wrappers.

How to get file_get_contents() to work with HTTPS? contains the following code to check for the registered wrappers:

$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);
Nico Haase
  • 11,420
  • 35
  • 43
  • 69
  • I've tried that and the output is as follows: openssl: yes http wrapper: yes https wrapper: yes wrappers: array ( 0 => 'https', 1 => 'ftps', 2 => 'compress.zlib', 3 => 'php', 4 => 'file', 5 => 'glob', 6 => 'data', 7 => 'http', 8 => 'ftp', 9 => 'compress.bzip2', 10 => 'phar', 11 => 'zip', ) The wrapper is on so it should be working – Pablo Adell Dec 21 '17 at 09:00
  • Okay, that's strange. Does the error log contain anything? – Nico Haase Dec 21 '17 at 09:06
  • 1
    Correction. I was missing a semicolon. Thanks for the help! – Pablo Adell Dec 21 '17 at 09:10