API reply content

  • 2
  • Idea
  • Updated 7 years ago
Here's a quick and dirty little bit of PHP for people that are trying to figure out the variables in the API reply:

function iterate($name, $object)
{
foreach ($object as $key => $value)
{
if (is_object($value))
{
iterate("${name}->${key}", $value);
}
else
if (is_array($value))
{
$n = count($value);
for ($i = 0; $i < $n; ++$i)
{
if (is_object($value[$i]))
iterate("${name}->${key}[$i]", $value[$i]);
else
print "${name}->${key}[$i] = '$value'
\n";
}
}
else
print "${name}->${key} = '$value'
\n";
}
}

$key = 'yourkey'; // your key here
$features = 'conditions/forecast/forecast10day/hourly10day'; // your desired features here
$query = 'location'; // your query here

$data = json_decode(file_get_contents("http://api.wunderground.com/api/$key/...;
iterate('data', $data);
Photo of bvipirate

bvipirate

  • 5 Posts
  • 1 Reply Like

Posted 7 years ago

  • 2
Photo of bvipirate

bvipirate

  • 5 Posts
  • 1 Reply Like
Well - the post came out poorly formatted.

Trying again:
function iterate($name, $object)
{
foreach ($object as $key => $value)
{
if (is_object($value))
{
iterate("${name}->${key}", $value);
}
else
if (is_array($value))
{
$n = count($value);
for ($i = 0; $i < $n; ++$i)
{
if (is_object($value[$i]))
iterate("${name}->${key}[$i]", $value[$i]);
else
print "${name}->${key}[$i] = '$value'
\n";
}
}
else
print "${name}->${key} = '$value'
\n";
}
}

$api = 'http://api.wunderground.com/api';
$key = 'yourkey'; // your key here
$features = 'conditions/forecast/forecast10day/hourly10day'; // your desired features here
$query = 'location'; // your query here
$data = json_decode(
file_get_contents("$api/$key/$features/q/$query.json")
);
iterate('data', $data);
?
Photo of bvipirate

bvipirate

  • 5 Posts
  • 1 Reply Like
OK - board does not like leading white space.
Photo of Brendan Hayes

Brendan Hayes, Official Rep

  • 962 Posts
  • 123 Reply Likes
Thanks for posting to share with the community!