Search code examples
phphtmlparsingfile-get-contents

Display external html specific value


I want to display the "hiOutsideTemp" value from this html page: http://amira.meteokrites.gr/ZWNTANA.htm to my page. It's a temp value.

I'm using the following code:

<?php

require_once 'simple_html_dom.php';
$html_string = file_get_contents('http://amira.meteokrites.gr/ZWNTANA.htm');
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($html_string);
libxml_clear_errors();
$xpath = new DOMXpath($dom);
$values = array();
$row = $xpath->query('//td[@id="/html/body/table/tbody/tr[7]/td[2]"]');
foreach($row as $value) {
    $values[] = trim($value->textContent);
}

echo '<pre>';
print_r($values);
?>

But i get no result. What shall i do?


Solution

  • Looking at the content of the URL you have, it just seems to be a list of settings and not a HTML page (this is only displayed if you don't have the actual HTML file included). The content is something like...

    imerominia="29/03/18";
    ora=" 9:37";
    sunriseTime=" 7:10";
    sunsetTime="19:37";
    ForecastStr=" Mostly cloudy and cooler.  Windy with possible wind shift to the W, NW, or N. ";
    tempUnit="°C";
    outsideTemp="9.3";
    hiOutsideTemp="9.5";
    hiOutsideTempAT=" 0:03";
    lowOutsideTemp="6.2";
    lowOutsideTempAT=" 4:24";
    ...
    

    So you can just load it as though it's an ini file format and this gives you an associative array of the data.

    $html_string = file_get_contents('http://amira.meteokrites.gr/ZWNTANA.htm');
    $data = parse_ini_string($html_string);
    
    echo $data["hiOutsideTemp"];  // outputs - 9.5