PHP 開發 APP 介面總結 - XML 方式封裝通訊介面

來源:互聯網
上載者:User

標籤:

1.PHP 產生 XML 資料

① 拼接字串

② 使用系統類別(DomDocument,XMLWriter,SimpleXML)

例1 使用 PHP 系統類別中的 DomDocument 類:

<?php$dom = new DomDocument(‘1.0‘,‘utf-8‘);$element = $dom->createElement(‘test‘,‘This is a root element‘);$dom->appendChild($element);echo $dom->saveXML();

頁面輸出

This is a root element

查看原始碼顯示:

<?xml version="1.0" encoding="utf-8"?><test>This is a root element</test>

例2 拼接字串

//修改 http 頭資訊header("Content-Type:text/xml");//xml頭資訊$xml = "<?xml version=‘1.0‘ encoding=‘utf-8‘?>\n";//根節點開始標籤$xml .= "<root>\n";//code$xml .= "<code>200</code>\n";    //message$xml .= "<message>資料返回成功</message>\n";    //data$xml .= "<data>\n";    $xml .= "<id>1</id>\n";$xml .= "<name>John</name>\n";$xml .= "</data>\n";//根節點結束標籤$xml .= "</root>";echo $xml;exit();

頁面輸出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.<root><code>200</code><message>資料返回成功</message><data><id>1</id><name>John</name></data></root>

http 回應標頭資訊:

 

2.XML 方式封裝通訊介面

<?phpclass Response{    /**    * 按 xml 方式輸出通訊資料    * @param integer $code 狀態代碼    * @param string $message 提示資訊    * @param array $data 資料    * return string    */    public static function xml($code,$message,$data){        if(!is_numeric($code)){            return ‘‘;        }        $result = array(            ‘code‘ => $code,            ‘message‘ => $message,            ‘data‘ => $data        );        //修改 http 頭資訊        header("Content-Type:text/xml");        //xml頭資訊        $xml = "<?xml version=‘1.0‘ encoding=‘utf-8‘?>";        //根節點開始標籤        $xml .= "<root>";        $xml .= self::xmlToEncode($result);        //根節點結束標籤        $xml .= "</root>";        echo $xml;        exit();    }    //解析$result至xml    public static function xmlToEncode($data){        $xml = $attr = "";        foreach($data as $k=>$v){            //如果$k是數字(data(code,message,data中的data)資料裡面還含有索引數組),要進行如下判斷            if(is_numeric($k)){                $attr = "id=‘{$k}‘";                $k = ‘item ‘;            }            $xml .= "<{$k} {$attr}>";            //如果$v是數組,則遞迴調用該方法            if(is_array($v)){                $xml .= self::xmlToEncode($v);            }else{                $xml .= $v;            }            $xml .= "</{$k}>";        }        return $xml;    }}

調用該頁面 test.php

$data 第一種情況:

<?phprequire ‘response.php‘;$data = array(    ‘id‘=>1,    ‘name‘=>‘Mary‘);Response::xml(200,‘資料返回成功‘,$data);

頁面輸出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.<root><code>200</code><message>資料返回成功</message><data><id>1</id><name>Mary</name></data></root>

$data 第二種情況

<?phprequire ‘response.php‘;$data = array(    ‘id‘=>1,    ‘name‘=>‘Mary‘,    ‘type‘=>array(1,3,6) //<0>1</0><1>3</1><2>6</2>  => <item id="0">1</item>...);Response::xml(200,‘資料返回成功‘,$data);

頁面輸出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.<root><code>200</code><message>資料返回成功</message><data><id>1</id><name>Mary</name><type><item id="0">1</item><item id="1">3</item><item id="2">6</item></type></data></root>

$data 第三中情況:

<?phprequire ‘response.php‘;$data = array(    ‘id‘=>1,    ‘name‘=>‘Mary‘,    ‘type‘=>array(‘a‘=>1,‘b‘=>3,‘c‘=>6));Response::xml(200,‘資料返回成功‘,$data);

頁面輸出:

This XML file does not appear to have any style information associated with it. The document tree is shown below.<root><code>200</code><message>資料返回成功</message><data><id>1</id><name>Mary</name><type><a>1</a><b>3</b><c>6</c></type></data></root>

 

PHP 開發 APP 介面總結 - XML 方式封裝通訊介面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.