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);
@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