蓝优小窝
蓝优
始于网络,终于现实
90后 狮子座 联系
这里以POST请求为例子:

客户端

<?php
function output()
{
    $Token = "这里是填写秘钥";
	$array = array("vid"=>9872);
	$api = PostCurl("接口地址", $array,$Token);
    $data = json_decode($api, true);
    return $data;
}
/**
 * @param {接口地址} $url
 * @param {请求参数} $data
 * @param {秘钥} $Token
 */
function PostCurl($url, $data,$Token)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Token:'.$Token));
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}
echo json_encode(output());

服务端

<?php
function output()
{
    $verify_Token = !empty($_SERVER['HTTP_TOKEN']) ? $_SERVER['HTTP_TOKEN'] : false;
    $vid = isset($_POST["vid"]) ? $_POST["vid"] : "";
    if (empty($verify_Token)) {
        exit(json_encode(Json_msg(201, "秘钥不能为空")));
    }
    if ($verify_Token == "ace888" && $vid == "9872") {
        $msg = Json_msg(200, "正常了");
    } else {
        $msg = Json_msg(202, "验证失败");
    }
    return $msg;
}
/**
 * @param {状态码} $code
 * @param {消息} $msg
 */
function Json_msg($code, $msg){
	return array("code"=>$code,"msg"=>$msg);
}

echo json_encode(output());
声明:若无特殊注明,本文为《蓝优》原创,转载请保留文章出处。
0 96