Section II Encapsulation Communication interface method

Source: Internet
Author: User
A. JSON way to encapsulate interface data methods (i) PHP generates JSON data

Methods: Json_encode ($value)
Note: This function can only accept UTF-8 encoded data, and if data in other data formats is passed, the function returns null (ii) standard format for communication data

Code Status code (200,400, etc.)
Message prompt (incorrect mailbox format; Data return success, etc.)
Data back

Define
class response{
    /
    * * Output communication data by JSON
    * @param integer $code  status Code
    * @param string  $ Message hint Info
    * @param array   $date  data
    *return string
    /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;
    }

? >
Call
require_once ('./json.php ');
    $arr = Array (
        ' id '    =>1,
        ' name '  => ' Singwa '
        );
    Response::json (200, ' data return success ', $arr);
two. XML mode encapsulates interface data methods (i) PHP generates XML data

1. Assemble string

public static function XML () {
    header ("Content-type:text/xml;charset=utf-8");
    $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>singwa</name>\n";
    $xml. = "</data>\n";
    $xml. = "</root>";
    echo $xml;
    }

2. Using System classes
①domdocument
②xmlwriter
③simplexml

# # (ii) XML encapsulates interface data methods
1. Encapsulation Method
Xmlencode ($code, $message = ', $data =array ())
2.data data Analysis
1.array (' index ' => ' API ');
2.array (1,7,89)

* * Output communication data by XML * @param integer $code Status code * @param string $message hint information * @param array $date data *return string/publi
    C 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;charset=utf-8 ');
    $xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '" >\n ";
    $xml. = "<root>\n";
    $xml. =self::xmltoencode ($result);
    $xml. = "</root>";
Echo $xml;
    The public static function Xmltoencode ($data) {$xml = "";  
    $attr = "";
            foreach ($data as $key => $value) {if (Is_numeric ($key)) {$attr = "id= ' {$key} '";
        $key = "Item";
        } $xml. = "<{$key} {$attr}>";
        If the array is recursive, if not, get $value $xml. =is_array ($value) Self::xmltoencode ($value): $value;
    $xml. = "</{$key}>\n";
}    return $xml;
}//Call $data = Array (' ID ' =>1, ' name ' => ' Singwa ', ' type ' =>array (4,5,6),); Response::xmlencode (' success ', $data);

three. The Integrated

is JSON or XML accessed via URL?
Example: Http://localhost/inter/json.php?format=xml

Class response{Const JSON = ' JSON '; * * output Communication data by JSON * @param integer $code Status code * @param string $message message * @param Array $date Data * @param string $type data type *return string/public static function show ($code, $message, $data =array (),
    $type =self::json) {if (!is_numeric ($code)) {return ';
    } $type =isset ($_get[' format ')? $_get[' format ']:self::json;
        $result = Array (' Code ' => $code, ' message ' => $message, ' data ' => $data,
    );
        if ($type = = ' json ') {Self::json ($code, $message, $data);
    Exit
        }elseif ($type = = ' array ') {var_dump ($result);
    Exit
        }elseif ($type = = ' xml ') {Self::xmlencode ($code, $message, $data);
    Exit   }else{//todo}/* * Output Communication data by JSON * @param integer $code Status code * @param string $message message * @param array
        $date data *return string/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;charset=utf-8");
    $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>singwa</name>\n";
    $xml. = "</data>\n";
    $xml. = "</root>";
Echo $xml; * * * Output communication data by XML * @param integer $code Status code * @param string $message message * @param array $date data *return string/Pub
    Lic 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;charset=utf-8 ');
    $xml = "<?xml version= ' 1.0 ' encoding= ' UTF-8 '" >\n ";
    $xml. = "<root>\n";
    $xml. =self::xmltoencode ($result);
    $xml. = "</root>";
Echo $xml;
    The public static function Xmltoencode ($data) {$xml = "";  
    $attr = "";
            foreach ($data as $key => $value) {if (Is_numeric ($key)) {$attr = "id= ' {$key} '";
        $key = "Item";
        } $xml. = "<{$key} {$attr}>";
        If the array is recursive, if not, get $value $xml. =is_array ($value) Self::xmltoencode ($value): $value;
    $xml. = "</{$key}>\n";
return $xml;
} $data = array (' ID ' =>1, ' name ' => ' Singwa ', ' type ' =>array (4,5,6),);
 Response::xmlencode (' success ', $data); Response::show (' success ', $data);

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.