zoukankan      html  css  js  c++  java
  • 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'?>
    ";
     81         $xml .= "<root>
    ";
     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}>
    ";
    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封装接口方法

     

     

    综合封装通信方法

     

  • 相关阅读:
    tcpdump抓localhost 127.0.0.1的包
    fatal: write failure on 'stdout': Bad file descriptor
    rst转html
    Call to WHvSetupPartition failed: ERROR_SUCCESS (Last=0xc000000d/87) (VERR_NEM_VM_CREATE_FAILED)
    error: 'readdir_r' is deprecated [-Werror=deprecated-declarations]
    ubuntu虚拟机设置共享后无权限
    VirtualBox怎么设置从u盘启动,虚拟机从U盘启动
    File类总结
    相对路径的写法
    systeminfo总结
  • 原文地址:https://www.cnblogs.com/bkwxx/p/12713032.html
Copyright © 2011-2022 走看看