PHP Classes

Accessing Yahoo Weather API

Recommend this page to a friend!

      PHP OAuth Library  >  All threads  >  Accessing Yahoo Weather API  >  (Un) Subscribe thread alerts  
Subject:Accessing Yahoo Weather API
Summary:Yahoo Return URL not Accepted
Messages:5
Author:Wayne Franklin
Date:2019-01-08 13:40:28
 

  1. Accessing Yahoo Weather API   Reply   Report abuse  
Picture of Wayne Franklin Wayne Franklin - 2019-01-08 13:40:28
Yahoo did away with their old api for weather as of Jan 03, 2019.

They offer an API that requires OAuth logins.

Here is the URL at Yahoo for creating the App ID, Client ID and Secret.

developer.yahoo.com/apps/create/?gu ...

When I try to put in a Return URL, it gives me an error creating the keys.

First error states you cannot use the name 'Yahoo' in the return URL path. So i changed it to the word 'weather'.

This is the error I get. Any Ideas???

Application Create failed. Scopes creation failed when creating App: 507 - {"domain_name":{"errors":[{"code":2402,"message":"Invalid domain name: swfsa.org\/login_with_weather.php"}]}}



  2. Re: Accessing Yahoo Weather API   Reply   Report abuse  
Picture of Wayne Franklin Wayne Franklin - 2019-01-08 15:14:10 - In reply to message 1 from Wayne Franklin
As you can see, I am NEW to APIs

This is what Yahoo wants in order to connect to the weather API.

GET /forecastrss?w=2502265 HTTP/1.1
Host: weather-ydn-yql.media.yahoo.com
Authorization: OAuth
oauth_consumer_key="YOUR_CONSUMER_KEY",oauth_signature_method="HMAC-SHA1",oauth_timestamp="YOUR_TIMESTAMP",oauth_nonce="YOUR_NONCE",oauth_version="1.0",oauth_signature="YOUR_GENERATED_SIGNATURE"
cache-control: no-cache

Not sure how to edit Login_With_Yahoo.php to make this all work.

I have all the IDs and a Call back domain activated in Yahoo.

Any ideas?

Thanks

  3. Re: Accessing Yahoo Weather API   Reply   Report abuse  
Picture of Wayne Franklin Wayne Franklin - 2019-01-08 16:27:57 - In reply to message 1 from Wayne Franklin
The Current Login_With_Yahoo uses a call that is now obsoleted by yahoo.

Here is the info from Yahoo:
Important EOL Notice: As of Thursday, Jan. 3, 2019, the weather.yahooapis.com and query.yahooapis.com for Yahoo Weather API will be retired.
To continue using our free Yahoo Weather APIs, use https://weather-ydn-yql.media.yahoo.com/forecastrss. Contact yahoo-weather-ydn-api@oath.com for credentials to onboard to this free Yahoo Weather API service.

Hope this helps,

Thanks

  4. Re: Accessing Yahoo Weather API   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2019-01-09 03:00:57 - In reply to message 3 from Wayne Franklin
Thanks Wayne.

Right now I do not have the time to take a look into this. Would you be willing to change the example and the Yahoo configuration settings and contribute those changes so I can make them available to all that need to deal with Yahoo integration?

  5. Re: Accessing Yahoo Weather API   Reply   Report abuse  
Picture of Wayne Franklin Wayne Franklin - 2019-01-23 18:42:19 - In reply to message 4 from Manuel Lemos
Here is a working version of the Yahoo Weather App using the Jan 2019 api from Yahoo.

It does not have a class associated with it as I am not yet able to understand how the OAuth Library works.

this is the code using structured programming:

<?php
// This oauth is provided by Yahoo on their api page for weather app
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';
// The next three lines of data are generated using the Yahoo Weather api app
// Complete the information on this page: https://developer.yahoo.com/weather/
$app_id = 'enter your app id here';
$consumer_key = 'enter consumer key here';
$consumer_secret = 'enter secret key here';

// Request Parameters
// Parameter Description Example
// location city name location=sunnyvale,ca
// lat latitude number lat=37.372
// lon longitude number lon=-122.038
// format response format format=xml (default) or format=json
// u unit format u=f (default) or u=c
// woeid woeid number woeid=2502265

// Enter the location you wish to get the weather for on the location line below or use the Request Parameters above
// Units for imperial and metric format are shown below.

// Imperial ("u=f") Metric ("u=c")
// Temperature Fahrenheit Celsius
// Distance Mile Kilometer
// Wind Direction Degree Degree
// Wind Speed Mile Per Hour Kilometer Per Hour
// Humidity Percentage Percentage
// Pressure Inch Hg Millibar

$query = array(
'location' => 'punta gorda,fl',
'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),
'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);

// There may be a better way to do this... I created a file of the json data so to use the file_get_contents command below
$myfile = fopen("data.json", "w") or die("Unable to open file!");
$txt = $response;
fwrite($myfile, $txt);
fclose($myfile);


$url = 'data.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data,true); // decode the JSON feed
echo "<br><br>" . "Decoded json data array" . "<br><br>";
print_r($characters); // print the array so that you can see all the elements
echo "<br><br>" . "Formated Output" . "<br><br>";


echo "<br><br>";
date_default_timezone_set("America/New_York");
echo "The Date and Time is " . date("m-d-Y h:i:s a") . " (America/New_York)" . "<br><br>";
echo "Location is " . $characters ['location'] ['city'] . ", " . $characters ['location'] ['region'] . " " . $characters ['location'] ['country'] . "<br><br>";
echo "Latitude and Longitude is " . $characters ['location'] ['lat'] . ", " . $characters ['location'] ['long'] . "<br><br>";
echo "Sunrise Time is " . $characters ['current_observation'] ['astronomy']['sunrise'] .
" and Sunset Time is ". $characters ['current_observation'] ['astronomy']['sunset'] . "<br><br>";

echo "Wind Speed (mph) Today is " . $characters ['current_observation'] ['wind']['speed'] . " and Wind Direction in Degrees is " .
$characters ['current_observation'] ['wind']['direction'] . "<br><br>";
echo "Atmosphere Pressure (Inch Hg) Today is " . $characters ['current_observation'] ['atmosphere']['pressure'] . " and Changing " .
$characters ['current_observation'] ['atmosphere']['rising'] . "<br><br>";

echo "Forecast for " . date(DATE_RFC822, $characters ['forecasts'] ['0']['date']) . "<br>" .
" Low Temperature (F) " . $characters ['forecasts'] ['0']['low'] . " degrees" . "<br>" .
" High Temperature (F) " . $characters ['forecasts'] ['0']['high'] . " degrees" . "<br>" .
" Condition " . $characters ['forecasts'] ['0']['text'] . "<br><br>";

echo "Looking ahead for the next 9 days" . "<br><br>";

echo "Forecast for " . date(DATE_RFC822, $characters ['forecasts'] ['1']['date']) . "<br>" .
" Low Temperature (F) " . $characters ['forecasts'] ['1']['low'] . " degrees" . "<br>" .
" High Temperature (F) " . $characters ['forecasts'] ['1']['high'] . " degrees" . "<br>" .
" Condition " . $characters ['forecasts'] ['1']['text'] . "<br><br>";

echo "Forecast for " . date(DATE_RFC822, $characters ['forecasts'] ['2']['date']) . "<br>" .
" Low Temperature (F) " . $characters ['forecasts'] ['2']['low'] . " degrees" . "<br>" .
" High Temperature (F) " . $characters ['forecasts'] ['2']['high'] . " degrees" . "<br>" .
" Condition " . $characters ['forecasts'] ['2']['text'] . "<br><br>";

echo "Forecast for " . date(DATE_RFC822, $characters ['forecasts'] ['3']['date']) . "<br>" .
" Low Temperature (F) " . $characters ['forecasts'] ['3']['low'] . " degrees" . "<br>" .
" High Temperature (F) " . $characters ['forecasts'] ['3']['high'] . " degrees" . "<br>" .
" Condition " . $characters ['forecasts'] ['3']['text'] . "<br><br>";

echo "Forecast for " . date(DATE_RFC822, $characters ['forecasts'] ['4']['date']) . "<br>" .
" Low Temperature (F) " . $characters ['forecasts'] ['4']['low'] . " degrees" . "<br>" .
" High Temperature (F) " . $characters ['forecasts'] ['4']['high'] . " degrees" . "<br>" .
" Condition " . $characters ['forecasts'] ['4']['text'] . "<br><br>";

echo "Forecast for " . date(DATE_RFC822, $characters ['forecasts'] ['5']['date']) . "<br>" .
" Low Temperature (F) " . $characters ['forecasts'] ['5']['low'] . " degrees" . "<br>" .
" High Temperature (F) " . $characters ['forecasts'] ['5']['high'] . " degrees" . "<br>" .
" Condition " . $characters ['forecasts'] ['5']['text'] . "<br><br>";

echo "Forecast for " . date(DATE_RFC822, $characters ['forecasts'] ['6']['date']) . "<br>" .
" Low Temperature (F) " . $characters ['forecasts'] ['6']['low'] . " degrees" . "<br>" .
" High Temperature (F) " . $characters ['forecasts'] ['6']['high'] . " degrees" . "<br>" .
" Condition " . $characters ['forecasts'] ['6']['text'] . "<br><br>";

echo "Forecast for " . date(DATE_RFC822, $characters ['forecasts'] ['7']['date']) . "<br>" .
" Low Temperature (F) " . $characters ['forecasts'] ['7']['low'] . " degrees" . "<br>" .
" High Temperature (F) " . $characters ['forecasts'] ['7']['high'] . " degrees" . "<br>" .
" Condition " . $characters ['forecasts'] ['7']['text'] . "<br><br>";

echo "Forecast for " . date(DATE_RFC822, $characters ['forecasts'] ['8']['date']) . "<br>" .
" Low Temperature (F) " . $characters ['forecasts'] ['8']['low'] . " degrees" . "<br>" .
" High Temperature (F) " . $characters ['forecasts'] ['8']['high'] . " degrees" . "<br>" .
" Condition " . $characters ['forecasts'] ['8']['text'] . "<br><br>";

echo "Forecast for " . date(DATE_RFC822, $characters ['forecasts'] ['9']['date']) . "<br>" .
" Low Temperature (F) " . $characters ['forecasts'] ['9']['low'] . " degrees" . "<br>" .
" High Temperature (F) " . $characters ['forecasts'] ['9']['high'] . " degrees" . "<br>" .
" Condition " . $characters ['forecasts'] ['9']['text'] . "<br><br>";


?>