1.客户端如何传递参数给服务端

 1.get请求,在url中拼接参数

 2.post请求 3种

                 a. Cont-type:applicaton/x-www-form-urlencode

                 b.Cont-type:multipart/form-data

                 b.Cont-type:applicaton/json;charset=UTF-8

2.服务端如何接受参数

  1.get请求,可以用$_GET();

   2.post中的前两种可以用$_POST(), 

     如果是json格式可以用file_get_content("php://input")获取原始输入数据流,然后再转为数组

     示例 $re = file_get_content("php://input");

             $reArr = json_decode($re,true);

3.接口返回json封装示例

/**
 * 使用trait是为了代码复用(理解不深)
 * Class ResponseJson
 */
trait ResponseJson{

    /**
     * 接口返回数据异常时
     * @param $code
     * @param $message
     * @param array $data
     * @return mixed|string
     */
    public function jsonData($code,$message,$data = []){
        return $this->jsonResponse($code,$message,$data);
    }
    /**
     * 接口成功时的返回
     * @param array $data
     * @return mixed|string
     */
    public function jsonSuccessData($data = []){
        return $this->jsonResponse(0,"success",$data);
    }
    /**
     * 返回json格式
     * @param $code
     * @param $message
     * @param $data
     * @return mixed|string
     */
    private function jsonResponse($code,$message,$data){
        $content =[
            'code' => $code,
            'msg' => $message,
            'data' => $data,
        ];
        return json_encode($content);
    }
}

4.传统web做验证

接口知识

 

 

相关文章:

  • 2022-01-10
  • 2021-07-02
  • 2022-02-25
  • 2022-12-23
  • 2021-12-21
  • 2021-07-07
猜你喜欢
  • 2021-10-19
  • 2021-04-12
  • 2021-12-02
  • 2021-11-18
  • 2021-09-25
  • 2021-09-03
相关资源
相似解决方案