PHP接口开发规范

参数传递方式:

所有参数key、value键值对,转json然后base64(urlencode(json))转码,传递给接口。

PHP接口端使用 file_get_contents('php://input') 接收后解码

PHP接收端

$data=file_get_contents('php://input');

$data=json_decode(urldecode(base64_decode($data)),true);

PHP发送端

function curl_post($curlHttp, $postdata, $isJson = true)

{

    $ch = curl_init(); //用curl发送数据给api

    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_URL, $curlHttp);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    if (!empty($isJson)) {

        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache"));

    }

    $response = curl_exec($ch);

    curl_close($ch);

    $result = json_decode($response, true);

    return $result;

}

前端基于jquery xhr请求

var data = {

        "a" : 1,

        "b" : 2,

        "c" : [

            {"x":1},

            {"x":2}

        ]

    };

    data = window.btoa(encodeURIComponent(JSON.stringify(data)));

    $.post("api_url",data,function(req){

    },'json');

参数验证规则:


<?php

/**

* 判定是否是正常的访问

* @deprecated 根据参数来进行判定访问的合法性

*/

function isValidRequire($requestkey = '')

{

    if (empty($requestkey)) {

        $requestkey = config('DEFAULT_REQUEST_KEY');

    }

    $tempParams = array();

    array_push($tempParams, $requestkey);

    $params = get_input();

    $publickey = $params['public_key']; // 内定的验证字段 publickey

    unset($params['public_key']);

    ksort($params);

    foreach ($params as $key => $value) {

        if (is_array($value)) {

            $value = json_encode($value);

        }

        array_push($tempParams, $key . '=' . $value);

    }

    $str_token = hash('md5', implode('&', $tempParams));

    if ($str_token != $publickey) {

        $this->error('非法访问');

    }

    return true;

}

/**

* 生成访问的安全验证token

* $params 参数

*/

function buildValidToken(array $params, $requestkey = '')

{

    $tempParams = array();

    if (empty($requestkey)) {

        $requestkey = config('DEFAULT_REQUEST_KEY');

    }

    array_push($tempParams, $requestkey);

    if (!empty($params)) {

        ksort($params);

        foreach ($params as $key => $value) {

            if (is_array($value)) {

                $value = json_encode($value);

            }

            array_push($tempParams, $key . '=' . $value);

        }

    }

    return hash('md5', implode('&', $tempParams));

}

function encode_data($arr)

{

    return base64_encode(urlencode(json_encode($arr)));

}

function get_input()

{

    $data = file_get_contents('php://input');

    return json_decode(urldecode(base64_decode($data)), true);

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,992评论 19 139
  • 点击查看原文 Web SDK 开发手册 SDK 概述 网易云信 SDK 为 Web 应用提供一个完善的 IM 系统...
    layjoy阅读 13,951评论 0 15
  •   2005 年,Jesse James Garrett 发表了一篇在线文章,题为“Ajax: A new App...
    霜天晓阅读 902评论 0 1
  • 前几日新闻爆出,39岁的詹小姐与某婚介公司签订服务合同之后,只相亲过一次,红娘便全部失联,该公司也关门,并被列入企...
    幸福空间阅读 188评论 0 0
  • 明明是搞笑的电影,但是却让我在大笑之余热泪盈眶。 沈腾式的幽默,就是那样吧。 王多鱼,王多鱼,^_^^_^
    新时代的我阅读 135评论 0 0