【问题标题】:How to display weather with this code如何使用此代码显示天气
【发布时间】:2016-12-26 08:39:27
【问题描述】:

好吧,我不会将这些代码归功于我发现它,但如果有人可以帮助我使用以下代码显示天气,我将不胜感激。

$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;

类似的东西。

非常感谢您宝贵的时间和善意的帮助

【问题讨论】:

  • 欢迎来到stackoveflow ..对于初学者...不要忘记包括您愿意为某人为您执行此操作支付的美元金额..哦等等..stackoverflow不是编码服务 - - 它是一个论坛,您可以在其中询问有关您的代码的问题并让其他人为您回答它们。不是I want someone to code this/that for me 论坛。对于初学者,请完整阅读stackoverflow.com/help/how-to-ask,了解如何礼貌地请求帮助并提出好的问题

标签: php curl weather


【解决方案1】:

对于 PHP 对象:

$phpObj =  json_decode($json);    // converting to object
echo $phpObj->property_name;

对于 PHP 数组:

$phpArr =  json_decode($json, true);    // converting to array
echo $phpArr['index'];

【讨论】:

  • 我不擅长,但我尝试回显 echo $phpObj['city'];然后我得到错误致命错误:不能使用stdClass类型的对象作为数组
  • 在 json_encode() 之后放一个 print_r() 并检查数据
  • $phpArr = json_decode($json, true); // 转换为数组 echo print_r($phpArr['index']);这样做我得到 1 被回显
【解决方案2】:

好的,我得到它并分享代码,以便它可以用于寻找天气 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/>';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-19
    • 2017-10-25
    • 2013-12-12
    • 2020-09-09
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多