Skip to content

Instantly share code, notes, and snippets.

@VerizonMediaOwner
Last active September 19, 2020 10:27
Show Gist options
  • Save VerizonMediaOwner/09d7d2115ec8b003055a6de652da2f79 to your computer and use it in GitHub Desktop.
Save VerizonMediaOwner/09d7d2115ec8b003055a6de652da2f79 to your computer and use it in GitHub Desktop.
Yahoo Weather API PHP Example
<?php
// Copyright 2019 Oath Inc. Licensed under the terms of the zLib license see https://opensource.org/licenses/Zlib for terms.
function buildBaseString($baseURI, $method, $params) {
$r = array();
ksort($params);
foreach($params as $key => $value) {
$r[] = "$key=" . rawurlencode($value);
}
return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}
function buildAuthorizationHeader($oauth) {
$r = 'Authorization: OAuth ';
$values = array();
foreach($oauth as $key=>$value) {
$values[] = "$key=\"" . rawurlencode($value) . "\"";
}
$r .= implode(', ', $values);
return $r;
}
$url = 'https://weather-ydn-yql.media.yahoo.com/forecastrss';
$app_id = 'your-app-id';
$consumer_key = 'your-consumer-key';
$consumer_secret = 'your-consumer-secret';
$query = array(
'location' => 'sunnyvale,ca',
'format' => 'json',
);
$oauth = array(
'oauth_consumer_key' => $consumer_key,
'oauth_nonce' => uniqid(mt_rand(1, 1000)),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
);
$base_info = buildBaseString($url, 'GET', array_merge($query, $oauth));
$composite_key = rawurlencode($consumer_secret) . '&';
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;
$header = array(
buildAuthorizationHeader($oauth),
'X-Yahoo-App-Id: ' . $app_id
);
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_HEADER => false,
CURLOPT_URL => $url . '?' . http_build_query($query),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
$return_data = json_decode($response);
print_r($return_data);
@yosemite-windows
Copy link

yosemite-windows commented Jan 11, 2019

Empty return, any idea what went wrong ?
{"location":{},"current_observation":{},"forecasts":[]}stdClass Object ( [location] => stdClass Object ( ) [current_observation] => stdClass Object ( ) [forecasts] => Array ( ) )

@Christian-hdr
Copy link

Christian-hdr commented Jan 11, 2019

First, thanks for the code sample.
I'm having the same issue as yosemite-windows.
The HTTP status code returned is 500, the OAuth part is working fine as i get an error 401 if I use wrong credentials.
I received the email confirming the access to the Weather API 15hours ago, is there any delay involved (I was already running some test before I got the mail and had the same error)

Edit : 3 days later the same code is working, seems to be a propagation delay

@klaasy1
Copy link

klaasy1 commented Jan 11, 2019

Hi all, I am also getting an empty forecasts json object, any help would be much appreciated. Thank you

@pierre2302
Copy link

Hello,

It seems that "?>" Is missing at the end of the file, but after adding it, it does not work either.
I also have this message:
{"location":{},"current_observation":{},"forecasts":[]}stdClass Object ( [location] => stdClass Object ( ) [current_observation] => stdClass Object ( ) [forecasts] => Array ( ) )

@ManojKiranA
Copy link

Empty return, any idea what went wrong ?
{"location":{},"current_observation":{},"forecasts":[]}stdClass Object ( [location] => stdClass Object ( ) [current_observation] => stdClass Object ( ) [forecasts] => Array ( ) )

yes it will because of the invalid location

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment