1

我在 yii 框架中工作。我正在使用 curl 扩展。我在 main.php 中包含了代码作为-

'curl'=>array(
    'class' => 'application.extensions.curl.Curl',
    ),

并在受保护/扩展名下包含 curl.php 文件。在控制器中,我将代码包含为=

public function actionGetWeather()
{
    $place='pune';
    $data = Yii::app()->curl->run('http://www.google.com/ig/api?weather='.$place.'&hl=en');

$xml = new SimplexmlElement($data);
foreach($xml->weather as $item) {

    foreach($item->current_conditions as $new) {

        //For temperature in fahrenheit replace temp_c by temp_f
        $current_temperature=$new->temp_c['data'];
        $current_humidity=$new->humidity['data'];
    }

    $current_condition=$item->forecast_conditions[0]->condition['data'];
    $next_temperature=$item->forecast_conditions[1]->high['data'];

    //to convert Fahrenheit into Celcius
    $next_temperature=round(($next_temperature-32)*(5/9));

    $next_condition=$item->forecast_conditions[1]->condition['data'];
}

echo " Current temperature :".$current_temperature."<br />";
echo "Current condition : ".$current_condition."<br />";
echo $current_humidity;
echo "Tomorrows temperature :".$next_temperature."<br />";
echo "Tomorrows condition : ".$next_condition."<br />";
}

但它给出了一个错误,因为 String 无法在“$xml = new SimplexmlElement($data);”行解析为 XML 那么我需要做些什么改变呢?请帮我

4

1 回答 1

0

可能 Google Weather Api 已关闭。试试http://worldweatheronline.com/weather api,几乎一样。

于 2013-04-21T06:59:11.423 回答