thinkphp6 移动云身份证验证
<?php
namespace app\api\service;
class IdCard
{
protected $accessKey="accessKey";
protected $secretKey="secretKey";
protected $host="https://api-zhengzhou-1.cmecloud.cn:8443";
//激活接口
public function Verifications($id, $name){
$requestPath = "/api/rp-cert/v1/id-cards/verifications";
$params=[];
$params['id'] = $id; //身份证号
$params['name'] = $name; //姓名
$ret = $this->pre_do_execute($params, $requestPath);
return $ret;
}
/**
* https://ecloud.10086.cn/op-help-center/doc/article/26253
*/
public function pre_do_execute($params, $requestPath){
$signatureNonce = md5($this->getSignatureNonce(16));
$signatureVersion = 'V2.0';
$signatureMethod = 'HmacSHA1';
// $timestamp = date('Y-m-d').'T'.date('H').'%3A'.date('i').'%3A'.date('s').'Z';
$timestamp = date('Y-m-d').'T'.urlencode(date('H:i:s')).'Z';
$version = '2016-12-05';
$queryString = 'AccessKey=' . $this->accessKey;
$queryString .='&SignatureMethod='.$signatureMethod;
$queryString .="&SignatureNonce=".$signatureNonce;
$queryString .="&SignatureVersion=".$signatureVersion;
$queryString .="&Timestamp=" . $timestamp;
// 文档中Version是必须的,postman 示例中没有这个参数(加不加测试着没影响)
$queryString .="&Version=".$version;
$sha256String = hash("sha256", $queryString);
$requestMethod = "POST";
$before=$requestMethod."\n".urlencode($requestPath)."\n".$sha256String;
$signature = $this->getSign($before,'BC_SIGNATURE&'.$this->secretKey);
$request_param=$queryString.'&Signature='.$signature;
$j_params = $params;
$j_params = json_encode($j_params,JSON_UNESCAPED_UNICODE);
$url=$this->host .$requestPath.'?'.$request_param;
$result = $this->httpPost($url, $j_params);
return $result;
}
public function getSignatureNonce($codeLenth)
{
$str_sn = '';
for ($i = 0; $i < $codeLenth; $i++)
{
if ($i == 0)
$str_sn .= rand(1, 9); // first field will not start with 0.
else
$str_sn .= rand(0, 9);
}
return $str_sn;
}
public function getSign($signStr,$signKey)
{
return hash_hmac("sha1", $signStr, $signKey);
}
public function httpPost($url, $param='' ,$header=[]){
// dump($url);
// dump($param);
// dump($header);
// exit();
$opts = array(
CURLOPT_TIMEOUT => 30,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
);
$opts[CURLOPT_URL] = $url;
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $param;
//发送JSON数据
if(empty($header)){
$header=[
'Content-Type: application/json',
// 'Content-Length: ' . strlen($param)
];
}
$opts[CURLOPT_HTTPHEADER] = $header;
/* 初始化并执行curl请求 */
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
//发生错误,抛出异常
if($error) throw new \Exception('请求发生错误:' . $error);
return $data;
}
}