Jump to content

Allowing both positive and negative values from query


calivw78

Recommended Posts

Hello all,

    I am currently working with some code that I found online to query the temperature from wunderground.com.  The problem that I am running into, is that the code does not appear to allow negative values.  Actually, I think it is simply not allowing the "-" to appear before the number.  I am not very fluent in php, but am reading plenty of resources.  None of those have helped me out yet though and my messing around with with the "value=" section of the below code has not resulted in the desired results.  Anyone have some thoughts?

 

Here is what I am currently using:

if (preg_match('#pwsvariable="tempf" english="°F" metric="°C" value="([0-9\.]+)">#', $page, $matches)) { $current_temp = $matches[1];

 

Thanks in advance.

For future reference, I fixed it by adjusting the code in my initial post to use

value="(-?[0-9\.]+)"

This allowed the negative character to be displayed.

 

In the end though, I found an easier way to get the values via XML

#!/usr/bin/php
#query weather data from wunderground xml for use with mrtg / rrdtool graphing utilities
<?php

//Get the XML document loaded into a variable
$ch = curl_init("http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=80013");

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = curl_exec($ch);
curl_close($ch);

$xml = new SimpleXMLElement($url);
echo $xml->temp_f."\n";
echo $xml->windchill_f."\n";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.