header ( "Content-type:text/html;Charset=utf-8" );
$url = "http://www.google.com/ig/api?weather=shenzhen"; //加载XML内容
$dom = new DOMDocument();
$dom->load ( $url );
$xpath = newDOMXPath ( $dom );
$element = $xpath->query ( "/xml_api_reply/weather/current_conditions" )->item ( 0 );
$condition = get_google_xml_data ( $element, "condition" );
$temp_c = get_google_xml_data ( $element, "temp_c" );
echo '天气:', $condition, '<br/>';
echo '温度:', $temp_c, '<br/>';
function get_google_xml_data($element, $tagname)
{
$tags = $element->getElementsByTagName ( $tagname );
//取得所有的$tagname if($items->length>1){
return $items;
}
$tag = $tags->item ( 0 ); //获取第一个以$tagname命名的标签
if($tag->hasAttributes ())
{ //获取data属性
$attribute = $tag->getAttribute ( "data" );
return $attribute;
}
else
{
returnfalse;
}
发现代码不能运行,提示有错误【"Warning: domdocument::domdocument() expects at least 1 parameter"】
解决如下:
问题的解决方法:http://bugs.php.net/bug.php?id=45538
php5 在 windows 下运行时,不需要在 php.ini 里声明 php_domxml.dll,加个分号注释起来就行了。
If you use PHP 5 in windows, you don't need to declare php_domxml.dll in your php.ini file.
so u can give comment in the line php_domxml.dll in your php.ini file.
you only need to comment it out, but do not delete the php_domxml.dll file in the ext directory.
评论