<?php
$name = '***';//姓名
$idNum = '***';//身份证号
$mobile = '***';// 手机号
$list = [
'name'=>$name,
'idNum'=>$idNum,
'mobile'=>$mobile,
];
$timestamp = time();
ksort($list);
// 创建用于签名的字符串
$str_for_sign = '';
foreach ($list as $key => $value) {
if ($str_for_sign !== '') {
$str_for_sign .= '&';
}
$str_for_sign .= $key . '=' . $value;
}
$secretKey = "应用分配secretKey";
$jsonList = json_encode($list,JSON_UNESCAPED_UNICODE);
$method = 'AES-256-ECB';
$encrypted = openssl_encrypt($jsonList, $method, $secretKey, OPENSSL_RAW_DATA);
$base64Encrypted = base64_encode($encrypted);
$signature = md5($base64Encrypted.'&'.$timestamp.'&'.$secretKey);
$url = "调用地址";
$data = [
'paramEncrypt'=>$base64Encrypted,
'reqEncryptType'=>1
];
$accessKey = "应用分配Access Key";
$headers = [
'Content-Type: application/json',
"appkey:$accessKey",
"timestamp:$timestamp",
"signature:$signature"
];
$result = sendRequest($url,$data,'json',$headers);
echo $result;
/**
* 发送请求
* */
function sendRequest($url, $postData, $type = 'json', $headers = array(
'Content-Type: application/json',
))
{
if ($type == 'json') {
$postData = json_encode($postData);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
因为百度智能云的示例只有Java,而且这个产品的接口都需要这样加密后才能调用成功
踩坑:文档表明Java使用的是AES-128加密,查了一下php的AES-256加密和JAVAAES-128的才是一样的