PHP Development APP Interface Summary-XML encapsulation of communication interfaces

Source: Internet
Author: User

1.PHP Generating XML data

① Stitching Strings

② using System classes (Domdocument,xmlwriter,simplexml)

Example 1 uses the DomDocument class in the PHP system class:

<? PHP $dom New DomDocument (' 1.0 ', ' utf-8 '); $element $dom->createelement (' Test ', ' this is a root element '); $dom->appendchild ($element); Echo $dom->savexml ();

Page output

This is a root element

View Source code display:

<? XML version= "1.0" encoding= "Utf-8" ?> < Test > This is a root element</test>

Example 2 stitching a string

//Modify HTTP header informationHeader("Content-type:text/xml");//XML Header Information$xml= "<?xml version= ' 1.0 ' encoding= ' utf-8 '? >\n";//root node start tag$xml. = "<root>\n";//Code$xml. = "<code>200</code>\n"; //message$xml. = "<message> Data return success </message>\n"; //Data$xml. = "<data>\n"; $xml. = "<id>1</id>\n";$xml. = "<name>john</name>\n";$xml. = "</data>\n";//root node end tag$xml. = "</root>";Echo $xml;Exit();

Page output:

This XML file does does appear to has any style information associated with it. The document tree is shown below.<Root><Code>200</Code><message>Data returned successfully</message><Data><ID>1</ID><name>John</name></Data></Root>

HTTP Response header information:

2.XML-Way encapsulated communication interface

<?PHPclassresponse{/** * Output communication data as XML * @param integer $code Status code * @param string $message message * @param array $data data * return string*/     Public Static functionxml$code,$message,$data){        if(!Is_numeric($code)){            return‘‘; }        $result=Array(            ' Code ' =$code, ' message ' =$message, ' data ' =$data        ); //Modify HTTP header information        Header("Content-type:text/xml"); //XML Header Information        $xml= "<?xml version= ' 1.0 ' encoding= ' utf-8 '?>"; //root node start tag        $xml. = "<root>"; $xml. = Self::xmltoencode ($result); //root node end tag        $xml. = "</root>"; Echo $xml; Exit(); }    //parsing $result to XML     Public Static functionXmltoencode ($data){        $xml=$attr= ""; foreach($data  as $k=$v){            //If $k is a number (the data in the Code,message,data also contains an indexed array), make the following judgment            if(Is_numeric($k)){                $attr= "Id= ' {$k}‘"; $k= ' Item '; }            $xml. = "<{$k} {$attr}> "; //if $v is an array, the method is called recursively            if(Is_array($v)){                $xml. = Self::xmltoencode ($v); }Else{                $xml.=$v; }            $xml. = "</{$k}> "; }        return $xml; }}

Call this page test.php

$data the first case:

<? PHP require ' response.php '; $data Array (    ' id ' =>1,    ' name ' = ' Mary '); Response:: XML (200, ' data return succeeded ',$data);

Page output:

file does not appear to has any style information associated with it. The document tree is shown below.<root><code>200</code><message> data return success </message>< Data><id>1</id><name>mary</name></data></root>

$data the second case

<? PHP require ' response.php '; $data Array (    ' id ' =>1,    ' name ' = ' Mary ',    ' type ' = +array//<0>1 </0><1>3</1><2>6</2>  = <item id= "0" >1</item> ... ); Response:: XML (200, ' data return succeeded ',$data);

Page output:

file does not appear to has any style information associated with it. The document tree is shown below.<root><code>200</code><message> data return success </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 the third situation:

<? PHP require ' response.php '; $data Array (    ' id ' =>1,    ' name ' = ' Mary ',    ' type ' = +array(' A ' =>1, ' B ' =>3, ' C ' =>6 )); Response:: XML (200, ' data return succeeded ',$data);

Page output:

file does not appear to has any style information associated with it. The document tree is shown below.<root><code>200</code><message> data return success </message>< Data><id>1</id><name>mary</name><type><a>1</a><b>3</b ><c>6</c></type></data></root>

PHP Development APP Interface Summary-XML encapsulation of communication interfaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.