PHP中的XML解析的5种方法
<p>【前言】<br> 不管是桌面软件开发,还是WEB应用,XML无处不在!<br> 然而在平时的工作中,仅仅是使用一些已经封装好的类对XML对于处理,包括生成,解析等。假期有空,于是将PHP中的几种XML解析方法总结如下:</p><p> 以解析Google API 接口提供的天气情况为例,我们取今天的天气及气温。<br> API地址:<span style="color: rgba(255, 102, 0, 1)">http://www.google.com/ig/api?weather=shenzhen</span></p>
<p>【XML文件内容】</p>
<div class="cnblogs_code">
<pre><?xml version="1.0"?>
<xml_api_reply version="1">
<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" >
<forecast_information>
<city data="Shenzhen, Guangdong"/>
<postal_code data="shenzhen"/>
<latitude_e6 data=""/>
<longitude_e6 data=""/>
<forecast_date data="2009-10-05"/>
<current_date_time data="2009-10-04 05:02:00 +0000"/>
<unit_system data="US"/>
</forecast_information>
<current_conditions>
<condition data="Sunny"/>
<temp_f data="88"/>
<temp_c data="31"/>
<humidity data="Humidity: 49%"/>
<icon data="/ig/images/weather/sunny.gif"/>
<wind_condition data="Wind:mph"/>
</current_conditions>
</weather>
</xml_api_reply></pre>
</div>
<p>【使用DomDocument解析】</p>
<div class="cnblogs_code">
<pre><?<span style="color: rgba(0, 0, 0, 1)">PHP
</span><span style="color: rgba(0, 128, 128, 1)">header</span>("Content-type:text/html; Charset=utf-8"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$url</span> = "http://www.google.com/ig/api?weather=shenzhen"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载XML内容</span>
<span style="color: rgba(128, 0, 128, 1)">$content</span> = <span style="color: rgba(0, 128, 128, 1)">file_get_contents</span>(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$content</span> = get_utf8_string(<span style="color: rgba(128, 0, 128, 1)">$content</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$dom</span> = DOMDocument::loadXML(<span style="color: rgba(128, 0, 128, 1)">$content</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
此处也可使用如下所示的代码,
$dom = new DOMDocument();
$dom->load($url);
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(128, 0, 128, 1)">$elements</span> = <span style="color: rgba(128, 0, 128, 1)">$dom</span>->getElementsByTagName("current_conditions"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$element</span> = <span style="color: rgba(128, 0, 128, 1)">$elements</span>->item(0<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> = get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, "condition"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$temp_c</span> = get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, "temp_c"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '天气:', <span style="color: rgba(128, 0, 128, 1)">$condition</span>, '<br />'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '温度:', <span style="color: rgba(128, 0, 128, 1)">$temp_c</span>, '<br />'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span> get_utf8_string(<span style="color: rgba(128, 0, 128, 1)">$content</span>) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将一些字符转化成utf8格式</span>
<span style="color: rgba(128, 0, 128, 1)">$encoding</span> = mb_detect_encoding(<span style="color: rgba(128, 0, 128, 1)">$content</span>, <span style="color: rgba(0, 0, 255, 1)">array</span>('ASCII','UTF-8','GB2312','GBK','BIG5'<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 0, 255, 1)">return</span>mb_convert_encoding(<span style="color: rgba(128, 0, 128, 1)">$content</span>, 'utf-8', <span style="color: rgba(128, 0, 128, 1)">$encoding</span><span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 0, 255, 1)">function</span> get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, <span style="color: rgba(128, 0, 128, 1)">$tagname</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(128, 0, 128, 1)">$tags</span> = <span style="color: rgba(128, 0, 128, 1)">$element</span>->getElementsByTagName(<span style="color: rgba(128, 0, 128, 1)">$tagname</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取得所有的$tagname</span>
<span style="color: rgba(128, 0, 128, 1)">$tag</span> = <span style="color: rgba(128, 0, 128, 1)">$tags</span>->item(0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取第一个以$tagname命名的标签</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$tag</span>->hasAttributes()) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取data属性</span>
<span style="color: rgba(128, 0, 128, 1)">$attribute</span> = <span style="color: rgba(128, 0, 128, 1)">$tag</span>->getAttribute("data"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$attribute</span><span style="color: rgba(0, 0, 0, 1)">;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
</span>?></pre>
</div>
<p> 这只是一个简单的示例,仅包括了loadXML, item, getAttribute,getElementsByTagName等方法,还有一些有用的方法,这个依据你的实际需要。</p>
<p>【XMLReader】<br> 当我们要用php解读xml的内容时,有很多物件提供函式,让我们不用一个一个字元去解析,而只要根据标签和属性名称,就能取出文件中的属性与内容了,相较之下方便许多。其中XMLReader循序地浏览过xml档案的节点,可以想像成游标走过整份文件的节点,并抓取需要的内容。</p>
<div class="cnblogs_code">
<pre><?<span style="color: rgba(0, 0, 0, 1)">PHP
</span><span style="color: rgba(0, 128, 128, 1)">header</span>("Content-type:text/html; Charset=utf-8"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$url</span> = "http://www.google.com/ig/api?weather=shenzhen"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载XML内容</span>
<span style="color: rgba(128, 0, 128, 1)">$xml</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XMLReader();
</span><span style="color: rgba(128, 0, 128, 1)">$xml</span>->open(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> = ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$temp_c</span> = ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">while</span> (<span style="color: rgba(128, 0, 128, 1)">$xml</span>-><span style="color: rgba(0, 0, 0, 1)">read()) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> echo $xml->name, "==>", $xml->depth, "<br>";</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$condition</span>) && !<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$temp_c</span><span style="color: rgba(0, 0, 0, 1)">)) {
</span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$xml</span>->name == 'condition' && <span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$condition</span>)) {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取第一个condition</span>
<span style="color: rgba(128, 0, 128, 1)">$condition</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>->getAttribute('data'<span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$xml</span>->name == 'temp_c' && <span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$temp_c</span>)) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取第一个temp_c</span>
<span style="color: rgba(128, 0, 128, 1)">$temp_c</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>->getAttribute('data'<span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(128, 0, 128, 1)">$xml</span>-><span style="color: rgba(0, 0, 0, 1)">read();
}
</span><span style="color: rgba(128, 0, 128, 1)">$xml</span>-><span style="color: rgba(0, 0, 0, 1)">close();
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '天气:', <span style="color: rgba(128, 0, 128, 1)">$condition</span>, '<br />'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '温度:', <span style="color: rgba(128, 0, 128, 1)">$temp_c</span>, '<br />';</pre>
</div>
<p> 我们只是需要取第一个condition和第一个temp_c,于是遍历所有的节点,将遇到的第一个condition和第一个temp_c写入变量,最后输出。</p>
<p>【DOMXPath】<br> 这种方法需要使用DOMDocument对象创建整个文档的结构,</p>
<div class="cnblogs_code">
<pre><?<span style="color: rgba(0, 0, 0, 1)">PHP
</span><span style="color: rgba(0, 128, 128, 1)">header</span>("Content-type:text/html; Charset=utf-8"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$url</span> = "http://www.google.com/ig/api?weather=shenzhen"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载XML内容</span>
<span style="color: rgba(128, 0, 128, 1)">$dom</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DOMDocument();
</span><span style="color: rgba(128, 0, 128, 1)">$dom</span>->load(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$xpath</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> DOMXPath(<span style="color: rgba(128, 0, 128, 1)">$dom</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$element</span> = <span style="color: rgba(128, 0, 128, 1)">$xpath</span>->query("/xml_api_reply/weather/current_conditions")->item(0<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> = get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, "condition"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$temp_c</span> = get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, "temp_c"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '天气:', <span style="color: rgba(128, 0, 128, 1)">$condition</span>, '<br />'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '温度:', <span style="color: rgba(128, 0, 128, 1)">$temp_c</span>, '<br />'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">function</span> get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, <span style="color: rgba(128, 0, 128, 1)">$tagname</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(128, 0, 128, 1)">$tags</span> = <span style="color: rgba(128, 0, 128, 1)">$element</span>->getElementsByTagName(<span style="color: rgba(128, 0, 128, 1)">$tagname</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取得所有的$tagname</span>
<span style="color: rgba(128, 0, 128, 1)">$tag</span> = <span style="color: rgba(128, 0, 128, 1)">$tags</span>->item(0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取第一个以$tagname命名的标签</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$tag</span>->hasAttributes()) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取data属性</span>
<span style="color: rgba(128, 0, 128, 1)">$attribute</span> = <span style="color: rgba(128, 0, 128, 1)">$tag</span>->getAttribute("data"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$attribute</span><span style="color: rgba(0, 0, 0, 1)">;
}</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
}
}
</span>?></pre>
</div>
<p>【xml_parse_into_struct】<br> 说明:int xml_parse_into_struct ( resource parser, string data, array &values [, array &index] )</p>
<p> 该函数将 XML 文件解析到两个对应的数组中,index 参数含有指向 values 数组中对应值的指针。最后两个数组参数可由指针传递给函数。<br> 注意: xml_parse_into_struct() 失败返回 0,成功返回 1。这和 FALSE 与 TRUE 不同,使用例如 === 的运算符时要注意。</p>
<div class="cnblogs_code">
<pre><?<span style="color: rgba(0, 0, 0, 1)">PHP
</span><span style="color: rgba(0, 128, 128, 1)">header</span>("Content-type:text/html; Charset=utf-8"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$url</span> = "http://www.google.com/ig/api?weather=shenzhen"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载XML内容</span>
<span style="color: rgba(128, 0, 128, 1)">$content</span> = <span style="color: rgba(0, 128, 128, 1)">file_get_contents</span>(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$p</span> = <span style="color: rgba(0, 128, 128, 1)">xml_parser_create</span><span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 128, 128, 1)">xml_parse_into_struct</span>(<span style="color: rgba(128, 0, 128, 1)">$p</span>, <span style="color: rgba(128, 0, 128, 1)">$content</span>, <span style="color: rgba(128, 0, 128, 1)">$vals</span>, <span style="color: rgba(128, 0, 128, 1)">$index</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">xml_parser_free</span>(<span style="color: rgba(128, 0, 128, 1)">$p</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '天气:', <span style="color: rgba(128, 0, 128, 1)">$vals</span>[<span style="color: rgba(128, 0, 128, 1)">$index</span>['CONDITION']]['attributes']['DATA'], '<br />'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '温度:', <span style="color: rgba(128, 0, 128, 1)">$vals</span>[<span style="color: rgba(128, 0, 128, 1)">$index</span>['TEMP_C']]['attributes']['DATA'], '<br />';</pre>
</div>
<p>【Simplexml】<br> 此方法在PHP5中可用<br> 这个在google的官方文档中有相关的例子,如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Charset: utf-8</span><span style="color: rgba(0, 128, 0, 1)">
/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 用php Simplexml 调用google天气预报api,和g官方的例子不一样
* google 官方php domxml 获取google天气预报的例子
* http://www.google.com/tools/toolbar/buttons/intl/zh-CN/apis/howto_guide.html
*
* @copyright Copyright (c) 2008 <cmpan(at)qq.com>
* @license New BSD License
* @version 2008-11-9
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 城市,用城市拼音</span>
<span style="color: rgba(128, 0, 128, 1)">$city</span> = <span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$_GET</span>['city']) ? 'shenzhen' : <span style="color: rgba(128, 0, 128, 1)">$_GET</span>['city'<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(128, 0, 128, 1)">$content</span> = <span style="color: rgba(0, 128, 128, 1)">file_get_contents</span>("http://www.google.com/ig/api?weather=<span style="color: rgba(128, 0, 128, 1)">$city</span>&hl=zh-cn"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$content</span> || <span style="color: rgba(0, 0, 255, 1)">die</span>("No such city's data"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$content</span> = mb_convert_encoding(<span style="color: rgba(128, 0, 128, 1)">$content</span>, 'UTF-8', 'GBK'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$xml</span> = <span style="color: rgba(0, 128, 128, 1)">simplexml_load_string</span>(<span style="color: rgba(128, 0, 128, 1)">$content</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$date</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>->weather->forecast_information->forecast_date-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$html</span> = <span style="color: rgba(128, 0, 128, 1)">$date</span>. "<br>\r\n"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$current</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>->weather-><span style="color: rgba(0, 0, 0, 1)">current_conditions;
</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> = <span style="color: rgba(128, 0, 128, 1)">$current</span>->condition-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$temp_c</span> = <span style="color: rgba(128, 0, 128, 1)">$current</span>->temp_c-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$humidity</span> = <span style="color: rgba(128, 0, 128, 1)">$current</span>->humidity-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$icon</span> = <span style="color: rgba(128, 0, 128, 1)">$current</span>->icon-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$wind</span> = <span style="color: rgba(128, 0, 128, 1)">$current</span>->wind_condition-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> && <span style="color: rgba(128, 0, 128, 1)">$condition</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>->weather->forecast_conditions->condition-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$icon</span> && <span style="color: rgba(128, 0, 128, 1)">$icon</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>->weather->forecast_conditions->icon-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$html</span>.= "当前: {<span style="color: rgba(128, 0, 128, 1)">$condition</span>}, {<span style="color: rgba(128, 0, 128, 1)">$temp_c</span>}°C,<img src='http://www.google.com/ig{<span style="color: rgba(128, 0, 128, 1)">$icon</span>}'/> {<span style="color: rgba(128, 0, 128, 1)">$humidity</span>} {<span style="color: rgba(128, 0, 128, 1)">$wind</span>} <br />\r\n"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">foreach</span>(<span style="color: rgba(128, 0, 128, 1)">$xml</span>->weather->forecast_conditions <span style="color: rgba(0, 0, 255, 1)">as</span> <span style="color: rgba(128, 0, 128, 1)">$forecast</span><span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(128, 0, 128, 1)">$low</span> = <span style="color: rgba(128, 0, 128, 1)">$forecast</span>->low-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$high</span> = <span style="color: rgba(128, 0, 128, 1)">$forecast</span>->high-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$icon</span> = <span style="color: rgba(128, 0, 128, 1)">$forecast</span>->icon-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> = <span style="color: rgba(128, 0, 128, 1)">$forecast</span>->condition-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$day_of_week</span> = <span style="color: rgba(128, 0, 128, 1)">$forecast</span>->day_of_week-><span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$html</span>.= "{<span style="color: rgba(128, 0, 128, 1)">$day_of_week</span>} : {<span style="color: rgba(128, 0, 128, 1)">$high</span>} / {<span style="color: rgba(128, 0, 128, 1)">$low</span>} °C, {<span style="color: rgba(128, 0, 128, 1)">$condition</span>} <img src='http://www.google.com/ig{<span style="color: rgba(128, 0, 128, 1)">$icon</span>}' /><br />\r\n"<span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 128, 1)">header</span>('Content-type: text/html; Charset: utf-8'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">print</span> <span style="color: rgba(128, 0, 128, 1)">$html</span><span style="color: rgba(0, 0, 0, 1)">;
</span>?></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/mzhaox/p/11294317.html
頁:
[1]