主頁 > 知識庫 > PHP創(chuàng)建XML接口示例

PHP創(chuàng)建XML接口示例

熱門標(biāo)簽:余姚電話機(jī)器人 百度地圖標(biāo)注偏差 廣東廣州在怎么申請400電話 電銷機(jī)器人問門薩維品牌my 咸寧銷售電銷機(jī)器人系統(tǒng) 外呼系統(tǒng)能給企業(yè)帶來哪些好處 百度地圖怎樣標(biāo)注圖標(biāo) 開發(fā)地圖標(biāo)注類網(wǎng)站 400電話蘭州申請請

本文實(shí)例講述了PHP創(chuàng)建XML接口的方法。分享給大家供大家參考,具體如下:

xml接口:即一個供用戶請求的頁面地址,該地址返回一個xml文檔信息。

下面的例子利用xml獲取最新的10條商品信息

?php
header("content-type:text/html;charset=utf-8");
//制作xml接口,提供網(wǎng)站最新的10條商品信息
$link = mysql_connect('localhost','root','123');
mysql_select_db('shop',$link);
mysql_query('set names utf8');
/*
shop>
  goods>
    name>/name>
    price>/price>
    number>/number>
  /goods>
  ......
/shop>
*/
$sql = "select goods_name, goos_price, goods_number from sw_goods order by goods_id desc limit 10";
$qry = mysql_query($sql);//接收的是一個結(jié)果集
$info = array();
//取出每一條記錄
while($re = mysql_fetch_assoc($qry)){
  //$re 是一維數(shù)組,代表每條記錄
  $info[] = $re;//$info是二維數(shù)組,接收每條記錄
}
$dom = new DOMDocument('1.0', 'utf-8');
$shop = $dom -> createElement('shop');//創(chuàng)建根節(jié)點(diǎn)
for($i=0; $icount($info); $i++){
  //創(chuàng)建元素節(jié)點(diǎn)
  $goods = $dom -> createElement('goods');
  $name = $dom -> createElement('name');
  $price = $dom -> createElement('price');
  $number = $dom -> createElement('number');
  //創(chuàng)建文本節(jié)點(diǎn)
  $name_txt = $dom -> createTextNode($info[$i]['goods_name']);
  $price_txt = $dom -> createTextNode($info[$i]['goods_price']);
  $number_txt = $dom -> createTextNode($info[$i]['goods_number']);
  //追加節(jié)點(diǎn)
  $name -> appendChild($name_txt);
  $price -> appendChild($price_txt);
  $number -> appendChild($number_txt);
  $goods -> appendChild($name);
  $goods -> appendChild($price);
  $goods -> appendChild($number);
  $shop -> appendChild($goods);
}
$dom -> appendChild($shop);//追加根節(jié)點(diǎn)
header("content-type:text/xml;charset=utf-8");
echo $dom -> saveXML();

PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

在線格式化XML/在線壓縮XML
http://tools.jb51.net/code/xmlformat

XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設(shè)計有所幫助。

您可能感興趣的文章:
  • php simplexmlElement操作xml的命名空間實(shí)現(xiàn)代碼
  • PHP XML操作類DOMDocument
  • php中DOMDocument簡單用法示例代碼(XML創(chuàng)建、添加、刪除、修改)
  • PHP使用DOMDocument類生成HTML實(shí)例(包含常見標(biāo)簽元素)
  • PHP 中 DOMDocument保存xml時中文出現(xiàn)亂碼問題的解決方案
  • PHP基于DOMDocument解析和生成xml的方法分析
  • PHP實(shí)現(xiàn)動態(tài)創(chuàng)建XML文檔的方法
  • PHP基于DOM創(chuàng)建xml文檔的方法示例
  • PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】

標(biāo)簽:鷹潭 臨沂 巴彥淖爾 麗江 重慶 銅陵 十堰 衡陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP創(chuàng)建XML接口示例》,本文關(guān)鍵詞  PHP,創(chuàng)建,XML,接口,示例,PHP,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP創(chuàng)建XML接口示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP創(chuàng)建XML接口示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章