PHP Development App Interface (i)

Source: Internet
Author: User

PHP is returned to the app in JSON or XML format. This is a good thing to know, to wrap the data in JSON or XML and back to the app

To define the abstract app base class:

<?php/** * Definition API abstract class */abstract class API {Const JSON = ' JSON '; const XML = ' XML '; const ARR = ' Array ';/*** define factory method * param s  Tring $type return data type */public static function factory ($type = Self::json) {$type = isset ($_get[' format ')? $_get[' format '): $type; $resultClass = Ucwords ($type); require_once ('./response/'. $type. '. php '); return new $resultClass ();} Abstract function response ($code, $message, $data);}

Return to the app in XML form:

<?phpclass Xml extends Api {public Function response ($code, $message = ", $data = Array ()) {if (!is_numeric ($code)) {RE Turn ';} $result = Array (' Code ' = $code, ' message ' = = $message, ' data ' = $data); header (' Content-type:text/xml '); $xml = " <?xml version= ' 1.0 ' encoding= ' UTF-8 '? >\n "; $xml. =" <root> "; $xml. = Self::xmltoencode ($result); $xml. =" </root> "; Echo $xml;} public static  function Xmltoencode ($result) {$xml = $attr = "; foreach ($result as $key + = $value) {                    //Determine key value pairs, if yes The numeric key value does not allow if (Is_numeric ($key)) {$attr = "id=". $key. "'"; $key = "Item";} $xml. = "<{$key} {$attr}>";                        Returned in a recursive form, mainly because the array is displayed in the XML as an array and must be displayed with specific key-value pairs of $xml. = Is_array ($value)? Self::xmltoencode ($value): $value; $xml. = "</{$key}>\n";} return $xml;}}

Return data in JSON format:

<?php/** * Output communication data as XML */class Json extends Api {public Function response ($code, $message = ', $data = Array ()) {if (! Is_numeric ($code)) {return ';} $result = Array (' Code ' = $code, ' message ' = = $message, ' data ' = $data); echo Json_encode ($result); exit;}}

You can also assemble the return data in this way:

<?phpclass Response {Const JSON = "JSON";/*** output communication data in an integrated manner * @param integer $code Status code * @param string $message message * @para M array $data 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 ') {//For debugging Code Var_dump ($result);} elseif ($type = = ' xml ') {Self::xmlencode ($code, $message, $data); exit;} else {//todo}}/*** output Communication data as JSON * @param integer $code status code * @par AM string $message hint * @param array $data data * return string*/public static function json ($code, $message = ', $data = a Rray ()) {if (!is_numeric ($code)) {return ';} $result = Array (' Code ' = $code, ' message ' = $message, ' data ' = $data); echo Json_encode ($result); exit;} /*** output Communication data as XML * @pAram Integer $code Status code * @param string $message message * @param array $data data * return string*/public static function xmlencod E ($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"; $xml. = Self::xmltoencode ($result); $xml. = " </root> "; Echo $xml;} public static function Xmltoencode ($data) {$xml = $attr = ""; foreach ($data as $key + = $value) {if (Is_numeric ($key)) {$a TTR = "id= ' {$key} '"; $key = "Item";} $xml. = "<{$key} {$attr}>"; $xml. = Is_array ($value)? Self::xmltoencode ($value): $value; $xml. = "</{$key}>\n";} return $xml;}}


PHP Development App Interface (i)

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.