Connect to the Yahoo! Weather API to get up-to-date weather information for a location. Test an API call in your browser and export the code snippet into your app.
The Yahoo! Weather API is free up to a certain number of calls: https://developer.yahoo.com/weather/
CSS Code:
.wraper{
width:60%;
margin: auto;
border: 1px solid silver;
display: flow-root;
}
.x_title{
text-align: center;
border-bottom: 1px solid silver;
padding-bottom: 20px;
}
.pull-left{
float: left;
}
.weather-text{
float: left;
padding-left: 50px
}
.degrees{
font-size: 58px;
display: inline-block
}
.degrees:after {
content: "o";
top: -31px;
font-size: 38px;
font-weight: 300;
position: relative;
}
.temperature{
padding-top: 20px;
text-align: center
}
.tempc{
display: inline-block;
font-size: 58px;
}
Php Code:
$wr = '<div class="wraper">
<div class="x_title">
<h2>Current weather</h2>
</div>
<div>
<div class="pull-left">
<div class="temperature"><b>'. date("l") .'</b>, '. date("h:i A") .'</div>
<img src="w.png">
</div>
<div class="weather-text">
<h2>'. $return_data->location->city .'<br><i>'. $return_data->location->region . ', ' . $return_data->location->country .'</i></h2>
<h3 class="degrees">'. round(($return_data->current_observation->condition->temperature - 32) * 5 / 9) .'</h3> <h2 class="tempc">C</h2>
</div>
</div>
</div>';
echo $wr;
PHP Code: With Yahoo API
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 = '';
$consumer_key = '';
$consumer_secret = '';
$query = array(
'location' => 'Sunnyvale,usa',
'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);
//echo ($response); exit;
$return_data = json_decode($response);
//print_r($return_data); exit;
date_default_timezone_set($return_data->location->timezone_id);