Last active
September 19, 2020 10:27
-
-
Save VerizonMediaOwner/09d7d2115ec8b003055a6de652da2f79 to your computer and use it in GitHub Desktop.
Yahoo Weather API PHP Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
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 ( ) )
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
Hi all, I am also getting an empty forecasts json object, any help would be much appreciated. Thank you