封装接口

<?php
header("Content-type: text/html; charset=utf-8");
//新建类
const APIKEY='shanzezhao';
class Client 
{
    //新建token方法
    /**
     * +--------------------------------------------------------------------------------------------------------------------
     * 秘钥生成器
     * +--------------------------------------------------------------------------------------------------------------------
     * @Author liwenxuan Date:2017/6/26
     * +--------------------------------------------------------------------------------------------------------------------
     * @access private
     * +--------------------------------------------------------------------------------------------------------------------
     * @type GET
     * +--------------------------------------------------------------------------------------------------------------------
     * @parame Array/String 必填 $data 需要加密的数组或字符串数组token为系统占用key
     * +--------------------------------------------------------------------------------------------------------------------
     * @parame Boolean 非必 $state (false:不产生随机key, true:产生随机key)
     * +--------------------------------------------------------------------------------------------------------------------
     * @return String $token 64位密文
     * +--------------------------------------------------------------------------------------------------------------------
    **/
    public function token($data=array()){
        $token='';//定义空变量
        if(is_array($data)){
            unset($data['token']);
            foreach ($data as $value) {
                $token.=$value;
            }
            $token=hash_hmac('md5', $token, APIKEY);
        }else{

            $token=hash_hmac('md5', $data,APIKEY);
        }
        return $token;

    }


    public function  curl($url, $data = null, $code,$time = 10){
            $data['token_time']=time();
            $data['token']=$this->token($data);
        if($code == 'get') {
            $url = $url.'?'.http_build_query($data);
        }
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);                                
        curl_setopt($ch, CURLOPT_HEADER, false);                            
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $time);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, false); 
        
        if($code == 'post') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }
        
        $ret = curl_exec($ch);
        curl_close($ch);
        $data = json_decode($ret, true);
        return $data;
        }
}
     $Client=new Client();
     $data['name']='王朋';
     $rr=$Client->curl('http://www.shanzezhao.com/appppppp/1/sapp.php',$data,'get',true);
     print_r($rr);

sapp.php

<?php
header("Content-type: text/html; charset=utf-8");
const APIKEY='shanzezhao';

class Servers 
{
    
    function __construct()
    {
        $token_time=time();      //获取当前时间
        $data = array();        //新数据$data        
        $data['token'] = '';

        if($_GET){
            $data=$_GET;
        }else{
            $data=$_POST;
        }

        $data['token']=trim($data['token']);

        if($data['token']!=$this->token($data)){
            exit(json_encode(array('data'=>'','message'=>'token验证不正确!','code'=>200)));
        }
        else {
            //将时间变量转成整数类型。 
            $data['token_time'] = intval($data['token_time']);
            //将时间加120
            $data['token_time'] += 120;
            //用于判断超时
            if($data['token_time'] <= $token_time) {    
        
                exit(json_encode(array('data' => array(), 'code'=>'201','message'=>'时间超时')));
            } 

        }
    }

     //token方法
         /**
     * +--------------------------------------------------------------------------------------------------------------------
     * 秘钥生成器
     * +--------------------------------------------------------------------------------------------------------------------
     * @Author liwenxuan Date:2017/6/26
     * +--------------------------------------------------------------------------------------------------------------------
     * @access private
     * +--------------------------------------------------------------------------------------------------------------------
     * @type GET
     * +--------------------------------------------------------------------------------------------------------------------
     * @parame Array/String 必填 $data 需要加密的数组或字符串数组token为系统占用key
     * +--------------------------------------------------------------------------------------------------------------------
     * @parame Boolean 非必 $state (false:不产生随机key, true:产生随机key)
     * +--------------------------------------------------------------------------------------------------------------------
     * @return String $token 64位密文
     * +--------------------------------------------------------------------------------------------------------------------
    **/
    private function token($data=array()) { 
        $token = '';

        //判断变量类型是否为数组类型
        if(is_array($data)) {
            //销毁指定的变量
            unset($data['token']);
            //遍历数组
            foreach($data as $val) {
                //连续定义变量
                $token .= $val;
            }
            //MD5加密
            $token = hash_hmac('md5', $token, APIKEY);
        } else {
            //不是数组MD5加密
            $token = hash_hmac('md5', $data, APIKEY); 
        }
            //返回token值
            return $token;
    }

    public function Info(){

        $data = array();
        $data = $_GET;
        return  json_encode(array('data' => $data, 'code'=>'200','message'=>'成功'));
    }

}
        $Servers=new Servers();
        echo  $Servers->Info();

 

转载于:https://www.cnblogs.com/yx520zhao/p/7121238.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值