<?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();