2016-03-26 36 views
2

我正在使用一個簡單的代碼,其中包括一個雅虎API代碼,只是從我的城市得到的天氣,並放在我的網頁上,但是,我只是讀雅虎公共API不再工作我不知道如何才能得到這個代碼工作,我有一個雅虎帳戶,我創建了一個API,我不知道如何從這裏開始。如果有人能幫助我,這是代碼:簡單的雅虎天氣Api不工作

<?php 
/*Clima*/ 
if(isset($_POST['zipcode']) && is_numeric($_POST['zipcode'])){ 
    $zipcode = $_POST['zipcode']; 
}else{ 
    $zipcode = 'ARMA0056'; 
} 
$result = file_get_contents('http://weather.yahooapis.com/forecastrss?p=' . $zipcode . '&u=c'); 
$xml = simplexml_load_string($result); 

//echo htmlspecialchars($result, ENT_QUOTES, 'UTF-8'); 

$xml->registerXPathNamespace('yweather', 'http://xml.weather.yahoo.com/ns/rss/1.0'); 
$location = $xml->channel->xpath('yweather:location'); 

if(!empty($location)){ 
    foreach($xml->channel->item as $item){ 
     $current = $item->xpath('yweather:condition'); 
     $forecast = $item->xpath('yweather:forecast'); 
     $current = $current[0]; 
     $clima = <<<END 
      <span>{$current['temp']}&deg;C</span> 

END; 
    } 
}else{ 
    $clima = '<h1>No results found, please try a different zip code.</h1>'; 
} 
/*Clima*/ 
?> 

回答

0

雅虎最近更新了他們處理請求的方式。它過去只是通過任何連接,但現在爲了使它更安全和更容易處理,他們最近選擇通過OAuth1發送所有請求。使用他們在其頁面上提供的示例代碼,並通過JSON從請求中獲取信息。

請參閱https://developer.yahoo.com/weather/瞭解更多信息。

+1

有時,'results'是'null',還與雅虎例子.. –

10

只是將http://weather.yahooapis.com替換爲http://xml.weather.yahoo.com。積分https://forum.rainmeter.net/viewtopic.php?f=13&t=23010

+0

我只是試圖讓XML請求一個Windows窗體應用程序,但我無法訪問第二個XML鏈接。例如,我試圖訪問http://xml.weather.yahoo.com/forecastrss?p=68510,它一直說它是無效的。 –

+0

嘗試用這個查詢'select * from weather.forecast where woeid in(select woeid from geo.places where placetype ='Zip'AND text ='68510')''https://query.yahooapis.com/v1/public /yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20%28select%20woeid%20from%20geo.places%20where%20placetype%3D%27Zip%27%20AND%20text%3D% 2768510%27%20%29&format = xml' – blackxel

+0

這只是停止工作,任何人都碰巧知道另一種方式? –

0

http://weather.yahooapis.com替換爲http://xml.weather.yahoo.com。例如,如果你點擊這個舊的鏈接*** 8http://weather.yahooapis.com/forecastrss?w = 2459115 & u = c & d = 5它會給你一個錯誤消息,要求有效的憑據OAuth 。 但是,如果你使用下面的鏈接,*** 8http://xml.weather.yahoo.com/forecastrss?w = 2459115 & u = c & d = 5,這將以xml格式顯示天氣數據,然後你可以按照您喜歡的方式解析所有給定的天氣數據。

注意:w = woied數字或當你在你的url中使用它p =郵編。兩者都給你相同的結果。如果你想獲得世界上任何一個城市的編號,只需轉到*** 8https://weather.yahoo.com/並選擇任何城市,你就會注意到url地址欄中顯示的編號。那個數字是那個特定城市的數字。 希望這有助於。

3

xml.weather.yahoo.com是解決方案,但該網址似乎不再有效。我現在使用yahoos查詢來獲取XML,即「https://query.yahooapis.com/v1/public/yql?q=select%20 *%20from%20weather.forecast%20where%20woeid%3D2489314」

這似乎是與添加到樹中的「結果」例外相同的XML。

0

YAHOO改變了一些關於api的規則; 我做了以下班級爲我工作...希望適合你; $ fcast = $ phpObj-> query-> results-> channel-> item-> forecast;改變這一行的其他項目...

<?php 
date_default_timezone_set('CET'); 
class weatherfc{ 
public $result; 
function weather($city){ 
$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="'.$city.'") and u="c"'; 
$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); 
    //var_dump($phpObj); 
    $weatherd='<div> Weather In '.$city.'<br>'; 
    $fcast=$phpObj->query->results->channel->item->forecast; 
    foreach($fcast as $witem){ 
    $fdate=DateTime::createFromFormat('j M Y', $witem->date); 

    $weatherd.= '<div class="days">'; 
    $weatherd.= '<div class="item"><div>'.$fdate->format('d.m').'&nbsp;'.$witem->day.'</div><div class="image" style="width:90px !important; height:65px !important;"><img src="http://us.i1.yimg.com/us.yimg.com/i/us/nws/weather/gr/'.$witem->code.'d.png" width=90></div></div>'; 
    $weatherd.= '<div><span>'.$witem->high.'&deg;C</span>'; 
    $weatherd.= '<span>'.$witem->low.'°C</span></div></div>'; 
    }; 
    $this->result=$weatherd; 
} 

} 

$h= new weatherfc; 
$h->weather("Antalya,Turkey"); 
echo $h->result; 
?> 



    <style> 
    .days{ 
    width:90px; 
    font-size:12px; 
    float:left; 
    font-family:Arial, Helvetica, sans-serif; 
    border:#999 1px dotted; 
    } 

</style>