php调用百度智能云号码认证接口-踩坑记录

<?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的才是一样的

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值