Rumah > Artikel > pembangunan bahagian belakang > PHP生成和获取XML格式数据_PHP教程
在做数据接口时,我们通常要获取第三方数据接口或者给第三方提供数据接口,而这些数据格式通常是以XML或者JSON格式传输,本文将介绍如何使用PHP生成XML格式数据供第三方调用以及如何获取第三方提供的XML数据。
生成XML格式数据
我们假设系统中有一张学生信息表student,需要提供给第三方调用,并有id,name,sex,age分别记录学生的姓名、性别、年龄等信息。
<ol class="dp-xml"> <li class="alt"><span><span>CREATE TABLE `student` ( </span></span></li> <li><span> `id` int(11) NOT NULL auto_increment, </span></li> <li class="alt"><span> `name` varchar(50) NOT NULL, </span></li> <li><span> `sex` varchar(10) NOT NULL, </span></li> <li class="alt"><span> `age` smallint(3) NOT NULL default '0', </span></li> <li><span> PRIMARY KEY (`id`) </span></li> <li class="alt"> <span>) </span><span class="attribute">ENGINE</span><span>=</span><span class="attribute-value">MyISAM</span><span> DEFAULT </span><span class="attribute">CHARSET</span><span>=</span><span class="attribute-value">utf8</span><span>; </span> </li> </ol>
首先,建立createXML.php文件,先连接数据库,获取数据。
<ol class="dp-sql"> <li class="alt"><span><span>include_once (</span><span class="string">"connect.php"</span><span>); //连接数据库 </span></span></li> <li> <span>$sql = </span><span class="string">"select * from student"</span><span>; </span> </li> <li class="alt"> <span>$result = mysql_query($sql) </span><span class="op">or</span><span> die(</span><span class="string">"Invalid query: "</span><span> . mysql_error()); </span> </li> <li><span>while ($row = mysql_fetch_array($result)) { </span></li> <li class="alt"><span> $arr[] = array( </span></li> <li> <span> </span><span class="string">'name'</span><span> => $row[</span><span class="string">'name'</span><span>], </span> </li> <li class="alt"> <span> </span><span class="string">'sex'</span><span> => $row[</span><span class="string">'sex'</span><span>], </span> </li> <li> <span> </span><span class="string">'age'</span><span> => $row[</span><span class="string">'age'</span><span>] </span> </li> <li class="alt"><span> ); </span></li> <li><span>} </span></li> </ol>
这个时候,数据就保存在$arr中,你可以使用print_r打印下数据测试。
接着,建立xml,循环数组,将数据写入到xml对应的节点中。
<ol class="dp-c"> <li class="alt"><span><span class="vars">$doc</span><span> = </span><span class="keyword">new</span><span> DOMDocument(</span><span class="string">'1.0'</span><span>, </span><span class="string">'utf-8'</span><span>); </span><span class="comment">// 声明版本和编码 </span><span> </span></span></li> <li> <span class="vars">$doc</span><span>->formatOutput = true; </span> </li> <li class="alt"> <span class="vars">$r</span><span> = </span><span class="vars">$doc</span><span>->createElement(</span><span class="string">"root"</span><span>); </span> </li> <li> <span class="vars">$doc</span><span>->appendChild(</span><span class="vars">$r</span><span>); </span> </li> <li class="alt"> <span class="keyword">foreach</span><span> (</span><span class="vars">$arr</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$dat</span><span>) { </span> </li> <li> <span> </span><span class="vars">$b</span><span> = </span><span class="vars">$doc</span><span>->createElement(</span><span class="string">"data"</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$name</span><span> = </span><span class="vars">$doc</span><span>->createElement(</span><span class="string">"name"</span><span>); </span> </li> <li> <span> </span><span class="vars">$name</span><span>->appendChild(</span><span class="vars">$doc</span><span>->createTextNode(</span><span class="vars">$dat</span><span>[</span><span class="string">'name'</span><span>])); </span> </li> <li class="alt"> <span> </span><span class="vars">$b</span><span>->appendChild(</span><span class="vars">$name</span><span>); </span> </li> <li> <span> </span><span class="vars">$sex</span><span> = </span><span class="vars">$doc</span><span>->createElement(</span><span class="string">"sex"</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$sex</span><span>->appendChild(</span><span class="vars">$doc</span><span>->createTextNode(</span><span class="vars">$dat</span><span>[</span><span class="string">'sex'</span><span>])); </span> </li> <li> <span> </span><span class="vars">$b</span><span>->appendChild(</span><span class="vars">$sex</span><span>); </span> </li> <li class="alt"> <span> </span><span class="vars">$age</span><span> = </span><span class="vars">$doc</span><span>->createElement(</span><span class="string">"age"</span><span>); </span> </li> <li> <span> </span><span class="vars">$age</span><span>->appendChild(</span><span class="vars">$doc</span><span>->createTextNode(</span><span class="vars">$dat</span><span>[</span><span class="string">'age'</span><span>])); </span> </li> <li class="alt"> <span> </span><span class="vars">$b</span><span>->appendChild(</span><span class="vars">$age</span><span>); </span> </li> <li> <span> </span><span class="vars">$r</span><span>->appendChild(</span><span class="vars">$b</span><span>); </span> </li> <li class="alt"><span>} </span></li> <li> <span class="func">echo</span><span> </span><span class="vars">$doc</span><span>->saveXML(); </span> </li> </ol>
我们调用了PHP内置的类DOMDocument来处理与生成xml。最终生成的xml格式请