php - SimpleXML child element attributes -
<form action='' method='post'> <input type='text' name='location'> <input type='submit' name='submit'> </form> <?php if(isset($_post['submit']) && !empty($_post['location'])) { $input = $_post['location']; $url = 'http://api.openweathermap.org/data/2.5/forecast?q='.strtolower($input).'&mode=xml'; $xml = file_get_contents($url, false); $xml = simplexml_load_string($xml); echo '<b>viewing weather for:</b> '. $xml->location->name; echo '<b>temperature:</b> '. $xml->forecast->children('temperature')->attributes('value'); } weather api: http://api.openweathermap.org/data/2.5/forecast?q=london,uk&mode=xml
i trying value of temperature
echo '<b>temperature:</b> '. $xml->forecast->children('temperature')->attributes('value'); this im stuck
i appreciate answers :)
to value attribute of first occurence of temperature node, do:
$result = $xml->forecast[0]->time[0]->temperature["value"]; to take <time> account, use xpath:
$results = $xml->xpath("//time"); this select time nodes, loop:
foreach ($results $result) echo "temperature $result[from] $result[to]: {$result->temperature['value']}<br />"; see working: http://codepad.viper-7.com/wuubev
Comments
Post a Comment