php app接口开发

 

 

 

 

 

 

 

 

 

实例:通过域名+文件名访问接口文件:app_api.com/response.php

  1 <?php
  2 
  3 class Response {
  4     const JSON = "json";
  5     /**
  6     * 按综合方式输出通信数据
  7     * @param integer $code 状态码
  8     * @param string $message 提示信息
  9     * @param array $data 数据
 10     * @param string $type 数据类型
 11     * return string
 12     */
 13     public static function show($code, $message = '', $data = array(), $type = self::JSON) {
 14         if(!is_numeric($code)) {
 15             return '';
 16         }
 17 
 18         $type = isset($_GET['format']) ? $_GET['format'] : self::JSON;
 19 
 20         $result = array(
 21             'code' => $code,
 22             'message' => $message,
 23             'data' => $data,
 24         );
 25 
 26         if($type == 'json') {
 27             self::json($code, $message, $data);
 28             exit;
 29         } elseif($type == 'array') {
 30             var_dump($result);
 31         } elseif($type == 'xml') {
 32             self::xmlEncode($code, $message, $data);
 33             exit;
 34         } else {
 35             // TODO
 36         }
 37     }
 38     /**
 39     * 按json方式输出通信数据
 40     * @param integer $code 状态码
 41     * @param string $message 提示信息
 42     * @param array $data 数据
 43     * return string
 44     */
 45     public static function json($code, $message = '', $data = array()) {
 46         
 47         if(!is_numeric($code)) {
 48             return '';
 49         }
 50 
 51         $result = array(
 52             'code' => $code,
 53             'message' => $message,
 54             'data' => $data
 55         );
 56 
 57         echo json_encode($result);
 58         exit;
 59     }
 60 
 61     /**
 62     * 按xml方式输出通信数据
 63     * @param integer $code 状态码
 64     * @param string $message 提示信息
 65     * @param array $data 数据
 66     * return string
 67     */
 68     public static function xmlEncode($code, $message, $data = array()) {
 69         if(!is_numeric($code)) {
 70             return '';
 71         }
 72 
 73         $result = array(
 74             'code' => $code,
 75             'message' => $message,
 76             'data' => $data,
 77         );
 78 
 79         header("Content-Type:text/xml");
 80         $xml = "<?xml version='1.0' encoding='UTF-8'?>\n";
 81         $xml .= "<root>\n";
 82 
 83         $xml .= self::xmlToEncode($result);
 84 
 85         $xml .= "</root>";
 86         echo $xml;
 87     }
 88 
 89     public static function xmlToEncode($data) {
 90 
 91         $xml = $attr = "";
 92         foreach($data as $key => $value) {
 93             if(is_numeric($key)) {
 94                 $attr = " id='{$key}'";
 95                 $key = "item";
 96             }
 97             $xml .= "<{$key}{$attr}>";
 98             $xml .= is_array($value) ? self::xmlToEncode($value) : $value;
 99             $xml .= "</{$key}>\n";
100         }
101         return $xml;
102     }
103 
104 }
View Code

在其他文件调用接口文件:test.php,访问test.php  app_api.com/test.php

1 <?php
2 require_once("./response.php");
3 
4 $data = array(
5     "name" => "bk",
6     "age" => 25
7 );
8 
9 Response::json(200,"返回成功",$data);
View Code

效果

 

 

 使用xml封装接口方法

 

 

 

 

 

 

综合封装通信方法

 

 

 

 

 

posted @ 2020-04-16 16:46  一个勤奋的程序员  阅读(316)  评论(0编辑  收藏  举报