PHP PHP Package JSON XML Universal app data communication interface for developing Android server

Source: Internet
Author: User
Tags package json
PHP File: test.php is a generic communication class

 $code, ' message ' = $message, ' data ' = $data, ' if ($type = = ' json ') {Self::json ($code, $message, $data); exit;} ElseIf ($type = = ' array ') {var_dump ($result);} elseif ($type = = ' xml ') {Self::xmlencode ($code, $message, $data); exit;} else {//todo}}/*** output Communication data as JSON * @param integer $code Status code * @param string $message message * @param array $data data * Return St Ring*/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;} /*** output Communication data as XML * @param integer $code Status code * @param string $message message * @param array $data data * return string*/public STA Tic 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 = "
 \ n "; $xml. ="
 
  
   \ n "; $xml. = Self::xmltoencode ($result); $xml. ="
  
 "; 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. = "
 \ n ";} return $xml;}}

Testxml&json.php generates XML and JSON by calling the show () method of the test class


 
  1, ' name ' = ' David '); Response::show (+, ' success ', $data);


This allows an app developer to choose whether to generate XML data or JSON data by assigning different values to the last parameter of the show () function.

Parameters for get requests over HTTP requests:

Http://localhost/testxml&json.php?format=xml This is the URL to generate the XML

  • Related Article

    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.