使用CURL扩展时的XML解析错误

时间:2012-12-21 12:35:59

标签: curl xml-parsing yii-extensions

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

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

并在protected / extensions下包含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无法在第34行解析为XML; $ xml = new SimplexmlElement($ data);&#34;那么我需要做出哪些改变?请帮帮我

1 个答案:

答案 0 :(得分:0)

Google Weather Api可能已经关闭。尝试http://worldweatheronline.com/天气api,它几乎相同。