New file |
| | |
| | | <?php |
| | | |
| | | |
| | | /** |
| | | * 对外接口工具类 Api |
| | | * 王腾宇 |
| | | */ |
| | | |
| | | // 1000001 微擎 |
| | | // 1000002 商城 |
| | | // 1000003 管理系统 |
| | | static $systemId = array( |
| | | "1000001"=>"3a03c2e6f2f911e8b9245254009bf9ba", |
| | | "1000002"=>"466a50d8f2f911e8b9245254009bf9ba", |
| | | "1000003"=>"53cfae5df2f911e8b9245254009bf9ba", |
| | | ); |
| | | |
| | | define("SHOP_URL", "http://shop.iemsoft.cn"); |
| | | |
| | | /** |
| | | * 接口数据校验 |
| | | * e00000 检验成功 |
| | | * e00001 接入平台不存在 |
| | | * e00002 鉴权失败 |
| | | */ |
| | | function checkParam ($param) |
| | | { |
| | | global $systemId; |
| | | $retData = array(); |
| | | $sysKey = $systemId[substr($param["id"],0,7)]; |
| | | if ($sysKey == null){ |
| | | $retData["regMsg"] = "e00001"; |
| | | } else { |
| | | if (checkSign($param,$sysKey)){ |
| | | $retData["regMsg"] = "e00000"; |
| | | $retData["regData"] = json_decode($param["data"],true); |
| | | } else { |
| | | $retData["regMsg"] = "e00002"; |
| | | } |
| | | } |
| | | return $retData; |
| | | } |
| | | |
| | | /** |
| | | * 校验签名 |
| | | */ |
| | | function checkSign($data,$sysKey) |
| | | { |
| | | $str = ""; |
| | | foreach ($data as $key => $value) |
| | | { |
| | | if ($key != "sign") { |
| | | $str = $str.$key."=".str_replace("\\", "", $value)."&"; |
| | | } |
| | | } |
| | | return $data["sign"] == md5($str."key=".$sysKey); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 解析请求接口地址 |
| | | */ |
| | | function getUrl($url) |
| | | { |
| | | $arr = explode('/', $url); |
| | | $end = !strpos($arr[count($arr)-1], '?') ? strlen($arr[count($arr)-1]) : strpos($arr[count($arr)-1], '?'); |
| | | return substr($arr[count($arr)-1],0,$end); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 生成对外接口字符串 |
| | | * 例子: id=100000120181128223494512&data=%7B%22code%22%3A%221%22%2C%22id%22%3A%22100000120181128223494512%22%7D&sign=a59336edda9fd6785fac6720d02c7d61 |
| | | */ |
| | | function makeInterfaceParam($param, $appId) |
| | | { |
| | | |
| | | $param["id"]=$appId.date("Ymd").getRandomNumber(9); |
| | | $paramJson = json_encode($param,JSON_UNESCAPED_UNICODE); |
| | | return |
| | | "id=".$param["id"]. |
| | | "&data=".urlencode($paramJson). |
| | | "&sign=".getSign($paramJson,$appId,$param["id"]); |
| | | } |
| | | |
| | | /** |
| | | * 生成签名 |
| | | */ |
| | | function getSign($param,$appId,$id){ |
| | | global $systemId; |
| | | return md5( |
| | | "id=".$id. |
| | | "&data=".$param. |
| | | "&key=".$systemId["$appId"] |
| | | ); |
| | | } |
| | | |
| | | /** |
| | | * 生成随机数字 |
| | | */ |
| | | function getRandomNumber($length){ |
| | | $arr = [0,1,2,3,4,5,6,7,8,9]; |
| | | $str = ''; |
| | | for ($i = 0; $i < $length; $i++) |
| | | { |
| | | $str .= $arr[rand(0, 9)]; |
| | | } |
| | | return $str; |
| | | } |
| | | |
| | | /** |
| | | * 生成openId |
| | | * @param $appId |
| | | * @return openId |
| | | */ |
| | | function getOpenId($appId) { |
| | | return $appId.date("Ymd").getRandomNumber(9); |
| | | } |
| | | |
| | | /** |
| | | *封闭curl的调用接口,get的请求方式。 |
| | | */ |
| | | function doCurlGetRequest($url,$data,$timeout = 5){ |
| | | if($url == "" || $timeout <= 0){ |
| | | return false; |
| | | } |
| | | $url = $url.'?'.http_build_query($data); |
| | | $con = curl_init((string)$url); |
| | | curl_setopt($con, CURLOPT_HEADER, false); |
| | | curl_setopt($con, CURLOPT_RETURNTRANSFER,true); |
| | | curl_setopt($con, CURLOPT_TIMEOUT, (int)$timeout); |
| | | |
| | | return curl_exec($con); |
| | | } |
| | | |
| | | /** |
| | | ** @desc 封装 curl 的调用接口,post的请求方式 |
| | | **/ |
| | | function doCurlPostRequest($url,$requestString,$timeout = 5){ |
| | | if($url == '' || $requestString == '' || $timeout <=0){ |
| | | return false; |
| | | } |
| | | $con = curl_init((string)$url); |
| | | curl_setopt($con, CURLOPT_HEADER, false); |
| | | curl_setopt($con, CURLOPT_POSTFIELDS, $requestString); |
| | | curl_setopt($con, CURLOPT_POST,true); |
| | | curl_setopt($con, CURLOPT_RETURNTRANSFER,true); |
| | | curl_setopt($con, CURLOPT_TIMEOUT,(int)$timeout); |
| | | if (strpos($url, 'https') !==false){ |
| | | curl_setopt($con, CURLOPT_SSL_VERIFYPEER, false); |
| | | } |
| | | $data = curl_exec($con); |
| | | if (curl_errno($con)) { |
| | | return curl_error($con); |
| | | } else { |
| | | curl_close($con); |
| | | return json_decode($data,true); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 指定key删除数组中指定的元素 |
| | | * @param unknown $data |
| | | * @param unknown $key |
| | | * @return unknown |
| | | */ |
| | | function array_remove($data, $key){ |
| | | if(!array_key_exists($key, $data)){ |
| | | return $data; |
| | | } |
| | | $keys = array_keys($data); |
| | | $index = array_search($key, $keys); |
| | | if($index !== FALSE){ |
| | | array_splice($data, $index, 1); |
| | | } |
| | | return $data; |
| | | } |