Php 如何使用此代码显示天气

Php 如何使用此代码显示天气,php,curl,weather,Php,Curl,Weather,嗯,我不会因为我发现的这些代码而受到表扬,但如果有人能帮我用下面的代码显示天气,我将非常感激 $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

嗯,我不会因为我发现的这些代码而受到表扬,但如果有人能帮我用下面的代码显示天气,我将非常感激

$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>';
像这样的

非常感谢您花宝贵的时间和好意帮助Php对象:

$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/>';
对于Php数组:


好的,我得到了它并分享了代码,这样它就可以对正在寻找天气api的人有用了


我不擅长,但我试着回应echo$phpObj['city'];然后我得到一个错误致命错误:在json编码并检查数据$phpArr=json\u decode$json后,无法使用stdClass类型的对象作为数组输入一个print\r,true;//转换为阵列回波打印_r$phpArr['index'];这样我就得到了1的回音。请来到stackoveflow。。首先。。。别忘了包括你愿意为某人为你做这件事支付的金额。。哦,等等。。stackoverflow不是一个编码服务——它是一个论坛,你可以在这里询问有关你的代码的问题,并让其他人为你解答。。不是一个我希望有人为我编写这个/那个论坛。首先,请通读全文,学习如何礼貌地请求帮助和提出好的问题
$phpObj =  json_decode($json);    // converting to object
echo $phpObj->property_name;
$phpArr =  json_decode($json, true);    // converting to array
echo $phpArr['index'];
$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/>';