PHP encapsulated XML Data Communication interface

Source: Internet
Author: User

PHP generates XML data
1) Assemble string: simple, easy to understand, commonly used
2) using System classes
Such as:
DomDocument

The following details describe the assembled string to generate XML data
XML data requires 1. Header Information <?xml
2. Root node
3. Data
<?php
Class response{
/**
* Output communication data in JSON mode
* @param integer $code status code
* @param string $message tip information
* @param array $data data
*return string return value is JSON
*/
Static methods, constructing JSON data

public static function XML () {
$xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>";
$xml. = "<root>";//Use a link to the front string, this is the root node
$xml. = "<code>200</code>";
$xml. = "<message> Data return success </message>";
$xml. = "<data>";
$xml. = "<id>1</id>";
$xml. = "<name>david</name>";
$xml. = "</data>";
$xml. = "</root>";


Echo $xml;


}
}
Response::xml ();
?>


Run Web page display:
200 Data Return Success 1david
View Source code:
<?xml version= ' 1.0 ' encoding= ' UTF-8 '?><root><code>200</code><message>


Data returned successfully


</message><data><id>1</id><name>david</name></data></root>


When viewing the source code in the browser, the data is close together, not intuitive. So each data output is followed by a newline.


$xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '? >\n";
$xml. = "<root>\n";//Use a link to the front string, this is the root node
$xml. = "<code>200</code>\n";
$xml. = "<message> Data return success </message>\n";
$xml. = "<data>\n";
$xml. = "<id>1</id>\n";
$xml. = "<name>david</name>\n";
$xml. = "</data>\n";
$xml. = "</root>\n";


After modification:
<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>
<root>
<code>200</code>
<message> Data Return Success </message>
<data>
<id>1</id>
<name>david</name>
</data>
</root>




By default: XML tags are not displayed in browser pages, only when viewing source code
Because the HTTP response header information is the HTML type by default. Default: Header ("content-


Type:text/html ");
1.png


If you need to display an XML tag on a page, you need to modify the content type in the response header information


Text/xml
public static function XML () {
Header ("Content-type:text/xml");
$xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '? >\n";
$xml. = "<root>\n";//Use a link to the front string, this is the root node
$xml. = "<code>200</code>\n";
$xml. = "<message> Data return success </message>\n";
$xml. = "<data>\n";
$xml. = "<id>1</id>\n";
$xml. = "<name>david</name>\n";
$xml. = "</data>\n";
$xml. = "</root>\n";


Echo $xml;


}






PHP XML method for encapsulating interface data
Encapsulation Method:
Xmlencode ($code, $message = "", $data =array ())




Complex array parsing does not come out:


<?php
Class response{

public static function Xmlencode ($code, $message, $data =array ()) {
if (!is_numeric ($code)) {
Return ';
}
$result =array (
' Code ' = $code,
' Message ' = $message,
' Data ' = $data,
);
Header ("Content-type:text/xml");
$xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '? >\n";
$xml. = "<root>\n";//Use a link to the front string, this is the root node
$xml. =self::xmltoencode ($result);
$xml. = "</root>\n";
Echo $xml;
}
public static function Xmltoencode ($data) {
Parse an array and assemble it into XML data
$xml = "";
foreach ($data as $key = = $value) {
$xml. = "<{$key}>";//curly braces can be identified as variables
$xml. = $value;
$xml. = "</{$key}>";
}
return $xml;
}


}
$data =array (
' id ' = 1,
' Name ' = ' David '
);
Response::xmlencode (+, ' success ', $data);
?>


Appear:
<root>
<code>200</code>
<message>success</message>
<data>Array</data>
</root>

When the Foreach loop is $value to the last data, it is not displayed, so it needs


Recursive invocation of Xmltoencode ($value)
It's about the code above.
$xml. = Is_$value; instead:
$xml. =is_array ($value)? Self::xmltoencode ($value): $value;
Results:
<root>
<code>200</code>
<message>success</message>
<data>
<id>1</id>
<name>david</name>
</data>
</root>


If the data to be encapsulated is not in key=>value form:
(A/D) automatically encapsulated into XML <0>1</0><1>2</1><2>2</2> numbers cannot be labeled so


Error
can be converted into:
<item id= "0" >1</item> form


Final version:
<?php
Class response{
/**
* Output communication data in JSON mode
* @param integer $code status code
* @param string $message tip information
* @param array $data data
*return string return value is JSON
*/
Static methods, constructing JSON data
/*public static function json ($code, $message = ", $data =array ()) {


if (!is_numeric ($code)) {
Return ';
}
$result =array (
' Code ' = $code,
' Message ' = $message,
' Data ' = $data
);
echo Json_encode ($result);
Exit
}*/
/*public static function xml () {
Header ("Content-type:text/xml");
$xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '? >\n";
$xml. = "<root>\n";//Use a link to the front string, this is the root node
$xml. = "<code>200</code>\n";
$xml. = "<message> Data return success </message>\n";
$xml. = "<data>\n";
$xml. = "<id>1</id>\n";
$xml. = "<name>david</name>\n";
$xml. = "</data>\n";
$xml. = "</root>\n";


Echo $xml;


}*/
public static function Xmlencode ($code, $message, $data =array ()) {
if (!is_numeric ($code)) {
Return ';
}
$result =array (
' Code ' = $code,
' Message ' = $message,
' Data ' = $data,
);
Header ("Content-type:text/xml");
$xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '? >\n";
$xml. = "<root>\n";//Use a link to the front string, this is the root node
$xml. =self::xmltoencode ($result);
$xml. = "</root>\n";
Echo $xml;
}
public static function Xmltoencode ($data) {
Parse an array and assemble it into XML data
$xml = $attr = "";

foreach ($data as $key = = $value) {
if (Is_numeric ($key)) {
$attr = "id= ' {$key} '";
$key = "Item";
}
$xml. = "<{$key} {$attr}>";//curly braces can be identified as variables
$xml. =is_array ($value)? Self::xmltoencode ($value): $value;
$xml. = "</{$key}>";
}
return $xml;
}


}
$data =array (
' id ' = 1,
' Name ' = ' David ',
' Type ' =>array (4,5,6)
);
Response::xmlencode (+, ' success ', $data);
?>

PHP encapsulated XML Data Communication interface

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.