如何使用此代码显示天气
How to display weather with this code
好吧,我不会把我发现的这些代码归功于它,但如果有人能帮助我用下面的代码显示天气,我将不胜感激。
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="sc")';
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
// Make call with cURL
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
// Convert JSON to PHP object
$phpObj = json_decode($json);
echo '<pre>';print_r($phpObj).'<pre>';
我只想让这段代码显示特定地点的天气以及一些我可以回显的变量
echo $city;
echo $temp;
像这样。
非常感谢您的宝贵时间和好意的帮助
对于Php对象:
$phpObj = json_decode($json); // converting to object
echo $phpObj->property_name;
对于Php数组:
$phpArr = json_decode($json, true); // converting to array
echo $phpArr['index'];
好的,我知道了并分享代码,以便它可以对正在寻找天气的人有用 api
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="sc")';
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
//Make call with cURL
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
// Convert JSON to PHP object
$phpObj = json_decode($json);
echo $phpObj->query->results->channel->location->city.' Weather <br/>';
echo 'Current: '.$phpObj->query->results->channel->item->condition->text.', ';
echo sprintf("%0.0f", ($phpObj->query->results->channel->item->condition->temp - 32) * 5 / 9).'°C <br/>';
echo $phpObj->query->results->channel->item->forecast[0]->day.': ';
echo $phpObj->query->results->channel->item->forecast[0]->text.', ';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[0]->low - 32) * 5 / 9).'Min°C - </small>';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[0]->high - 32) * 5 / 9).'Max°C </small><br/>';
echo $phpObj->query->results->channel->item->forecast[1]->day.': ';
echo $phpObj->query->results->channel->item->forecast[1]->text.', ';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[1]->low - 32) * 5 / 9).'Min°C - </small>';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[1]->high - 32) * 5 / 9).'Max°C </small><br/>';
好吧,我不会把我发现的这些代码归功于它,但如果有人能帮助我用下面的代码显示天气,我将不胜感激。
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="sc")';
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
// Make call with cURL
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
// Convert JSON to PHP object
$phpObj = json_decode($json);
echo '<pre>';print_r($phpObj).'<pre>';
我只想让这段代码显示特定地点的天气以及一些我可以回显的变量
echo $city;
echo $temp;
像这样。
非常感谢您的宝贵时间和好意的帮助
对于Php对象:
$phpObj = json_decode($json); // converting to object
echo $phpObj->property_name;
对于Php数组:
$phpArr = json_decode($json, true); // converting to array
echo $phpArr['index'];
好的,我知道了并分享代码,以便它可以对正在寻找天气的人有用 api
$BASE_URL = "http://query.yahooapis.com/v1/public/yql";
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="sc")';
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
//Make call with cURL
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
// Convert JSON to PHP object
$phpObj = json_decode($json);
echo $phpObj->query->results->channel->location->city.' Weather <br/>';
echo 'Current: '.$phpObj->query->results->channel->item->condition->text.', ';
echo sprintf("%0.0f", ($phpObj->query->results->channel->item->condition->temp - 32) * 5 / 9).'°C <br/>';
echo $phpObj->query->results->channel->item->forecast[0]->day.': ';
echo $phpObj->query->results->channel->item->forecast[0]->text.', ';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[0]->low - 32) * 5 / 9).'Min°C - </small>';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[0]->high - 32) * 5 / 9).'Max°C </small><br/>';
echo $phpObj->query->results->channel->item->forecast[1]->day.': ';
echo $phpObj->query->results->channel->item->forecast[1]->text.', ';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[1]->low - 32) * 5 / 9).'Min°C - </small>';
echo '<small>'.sprintf("%0.0f", ($phpObj->query->results->channel->item->forecast[1]->high - 32) * 5 / 9).'Max°C </small><br/>';