用PHP的DOM控件來創建輸出
輸出的格式為XML
接口開發的相關文件及說明
<?php header("Content-type: text/xml");//頭文件非常重要 //創建xml文件 $dom=new DOMDocument('1.0','utf-8'); //建立<root>元素 $root=$dom->createElement('root'); //把<root>元素追加到文檔里面 $dom->appendChild($root); //建立<book>元素並將其作為<root>的子元素 $book=$dom->createElement('book'); $root->appendChild($book); //接受GET過來的數據 $key=$_GET['key']; $name=$_GET['name']; //引入數據庫配置文件 include("config.inc.php"); //將get來的數據與數據庫里的數據進行校驗 $sql1=mysql_query("select count(id) as sum from user where `key` = ".$key.""); //返回結果集 $result=mysql_fetch_array($sql1); //如果數據庫里存在key if($result[0]>0){ //校驗name $sql2=mysql_query("select * from book where `name` = '".$name."'"); $row=mysql_fetch_row($sql2); if(mysql_num_rows($sql2)>0){ //為<book>創建name元素,並追加name的值 $name=$dom->createElement('name'); $nameText=$dom->createTextNode($row[1]); $name->appendChild($nameText); $book->appendChild($name); //為<book>創建author元素,並追加author的值 $author=$dom->createElement('author'); $authorText=$dom->createTextNode($row[2]); $author->appendChild($authorText); $book->appendChild($author); //為<book>創建press元素,並追加press的值 $press=$dom->createElement('press'); $pressText=$dom->createTextNode($row[3]); $press->appendChild($pressText); $book->appendChild($press); //為<book>創建price元素,並追加prcie的值 $price=$dom->createElement('price'); $priceText=$dom->createTextNode($row[4]); $price->appendChild($priceText); $book->appendChild($price); //為<book>創建time元素,並追加time的值 $time=$dom->createElement('time'); $timeText=$dom->createTextNode($row[5]); $time->appendChild($timeText); $book->appendChild($time); //為<book>創建intro元素,並追加intro的值 $intro=$dom->createElement('intro'); $introText=$dom->createTextNode($row[6]); $intro->appendChild($introText); $book->appendChild($intro); }else{//如果name不存在 //建立<error_code>,並追加error_code的值 $error_code=$dom->createElement('error_code'); $error_code_Text=$dom->createTextNode('錯誤代碼1001'); $error_code->appendChild($error_code_Text); $book->appendChild($error_code); //建立<result>,並追加result的值 $result=$dom->createElement('result'); $result_Text=$dom->createTextNode('書名不存在或錯誤'); $result->appendChild($result_Text); $book->appendChild($result); } }else{//如果key不存在 //建立<error_code>,並追加error_code的值 $error_code=$dom->createElement('error_code'); $error_code_Text=$dom->createTextNode('錯誤代碼1002'); $error_code->appendChild($error_code_Text); $book->appendChild($error_code); //建立<result>,並追加result的值 $result=$dom->createElement('result'); $result_Text=$dom->createTextNode('密鑰錯誤或不存在'); $result->appendChild($result_Text); $book->appendChild($result); } //在一字符串變量中建立XML結構 $xmlText=$dom->saveXML(); //輸出XML字符串 echo $xmlText; ?>
傳參key=12345,name=C語言。
然后輸出的結果是:
這里是用戶用javascript調用xml文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN"> <head> <title>獲取數據</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="description" content="" /> <meta name="keywords" content="" /> <script type="text/javascript"> window.onload=function(){ var btn=document.getElementById('btn'); btn.onclick=function(){ var title=document.getElementById('title').value; var name,author,press,price,book; if(window.XMLHttpRequest){ //code for IE+7,Firefox,Chrome,Opera,Safari xmlhttp=new XMLHttpRequest(); }else{ //code foe IE6,IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHttp"); } url='http://localhost/api/index.php?key=12345&name='+title; //console.log(url); xmlhttp.open("GET",url,false); xmlhttp.send(); xmldoc=xmlhttp.responseXML; book=xmldoc.getElementsByTagName("book"); error_code=xmldoc.getElementsByTagName("error_code"); if(error_code.length==1){ alert("書名錯誤或不存在"); } name=xmldoc.getElementsByTagName("name")[0]; author=xmldoc.getElementsByTagName("author")[0]; press=xmldoc.getElementsByTagName("press")[0]; price=xmldoc.getElementsByTagName("price")[0]; text="<table border='1'><tr><th>書名</th><th>作者</th><th>出版社</th><th>價格</th></tr>"; text+="<tr>"; text+="<td>"+name.firstChild.nodeValue+"</td>"; text+="<td>"+author.firstChild.nodeValue+"</td>"; text+="<td>"+press.firstChild.nodeValue+"</td>"; text+="<td>"+price.firstChild.nodeValue+"</td>"; text+="</tr>"; text+="</table>"; document.getElementById("text").innerHTML=text; } } </script> <style type="text/css"> </style> </head> <body> 書名:<input type="text" name="title" id="title"/> <input type="submit" id="btn" value="查詢"/> <div id="text"></div> </body> </html>
這里是接口開發的相關代碼:
http://download.csdn.net/detail/yxhbk/9506715
這里是接口開發說明文檔:
http://wenku.baidu.com/view/14706f8369eae009591bec44