简单的雅虎天气Api不工作

时间:2016-03-26 23:26:22

标签: api yahoo weather

我使用的是一个简单的代码,其中包含一个yahoo api代码,以便从我的城市获取天气并放入我的网页,但是,我只是读到雅虎公共api不再工作,我不知道怎么能ai得到这个代码工作,我有一个雅虎帐户,我创建了一个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*/
?> 

4 个答案:

答案 0 :(得分:10)

只需将http://weather.yahooapis.com替换为http://xml.weather.yahoo.com即可。积分到https://forum.rainmeter.net/viewtopic.php?f=13&t=23010

答案 1 :(得分:3)

xml.weather.yahoo.com是解决方案,但URL似乎不再起作用了。我现在使用yahoos查询获取XML,即#34; https://query.yahooapis.com/v1/public/yql?q=select%20 *%20from%20weather.forecast%20 where%20woeid%3D2489314&#34;

这似乎是相同的XML,除了&#34;结果&#34;添加到树上。

答案 2 :(得分:0)

雅虎最近更新了他们处理请求的方式。它过去只是通过任何连接,但现在为了使其更安全和更容易处理,他们最近选择通过OAuth1发送所有请求。使用他们在页面上提供的示例代码,并通过JSON从请求中获取信息。

有关详细信息,请参阅https://developer.yahoo.com/weather/

答案 3 :(得分:0)

YAHOO改变了一些关于api的规则; 我让以下课程为我工作......希望对你有用; $ fcast = $ phpObj-&GT;查询 - &GT;结果&GT;通道 - &GT;本期特价货品&GT;预测;将此行更改为其他项目......

<?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>