Encapsulate the Communication interface data method:
1.json method of encapsulating data
2.xml method of encapsulating data
3. Comprehensive data encapsulation method.
JSON mode: Json_encode ();
The function only accepts UTF-8 encoded data;
Iconv (' Primitive code ', ' Target code ', ' variable ');
XML mode
PHP generates XML data:
1> Assembling strings
2> using System Classes
DomDocument
<?php
$dom = new DOMDocument (' 1.0 ', ' utf-8 ');
$element = $dom->createelement (' Test ', ' This is the root element! ');
$dom->appendchild ($element);
echo $dom->savexml ();
?>
XMLWriter
SimpleXML
The node of XML cannot be a number, so the index array will give an error;
Solution:
<0>1</0>
===><item id= ' 0 ' >1</item>
Standard format for communication data:
Code Status code (200,400, etc.)
Message prompt message (incorrect mailbox format, data return not successful, etc.)
Data returned
<?php
class response{
/**
* Output communication data in JSON mode
* @param integert $code Status Code
* @param string $message tip information
* @param array $data data
*return String
*/
Public static function json ($code, $message = ", $data =array ()) {
if (!is_numeric ($code)) {
return "";
}
$result = Array (
' Code ' = $code,
' message ' = $message,
' data ' = $data
}
return Json_encode ($result);
}
Public static function xml () {
header (' Content-type:text/xml ');
$xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '?> \ n";
$xml. = "<root> \ n";
$xml. = "<code>200</code>\n";
$xml. = "<message> Data return success </message>\n";
$xml. = "<data>\n";
$xml. = "<id>1</id>\n";
$xml. = "<name>xiaoming</name>\n";
$xml. = "</data>\n";
$xml. = "</root>";
}
/**
* Output communication data as XML
* @param integert $code Status Code
* @param string $message tip information
* @param array $data data
*return String
*/
Public static function Xmlencode ($code, $message = ", $data) {
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";
$xml. =self::xmltoencode ($result);
$xml. = "</root>";
}
Public static function Xmltoencode ($data) {
$xml = $attr = "";
foreach ($data as $key = = $value) {
if (Is_numeric ($key)) {
$attr = "id= ' {$key} '";
$key = "item";
}
$xml. = "<{$key} {$attr}>\n";
$xml. =is_array ($value)? Self::xmltoencode ($value): $value;
$xml. = "</{$key}> \ n";
}
return $xml;
}
/**
* Output communication data in an integrated manner
* @param integert $code Status Code
* @param string $message tip information
* @param array $data data
* @param string $type data type
*return String
*/
Public static function show ($code, $message = ", $data =array (), $type = ' json ') {
if (!is_numeric ($code)) {
return "";
}
$type =isset ($_get[' format ')? $_get[' format ']: $type;
$result =array (
' Code ' = $code,
' message ' = $message,
' data ' = $data
)
if ($type = = ' json ') {
Self::json ($code, $message, $data);
exit;
}elseif ($type = = ' array ') {
return $result;
}elseif ($type = = ' xml ') {
Self::xmlencode ($code, $message, $data);
}else{
//todo
}
}
}
?>
=================================================================
<?php
/* Direct Call */
Require_once ('./reponse.php ');
$arr = array{
' ID ' =>1,
' Name ' = ' xiaoming '
)
Reponse::json (200, ' data return success ', $arr);
Reponse::xmlencode (200, ' data return success ', $arr);
?>
PHP Development App Interface (III)