thinkphp发送百度短信的代码
第一步,在/config新建sms.php,设置短信密钥,直接写在控制器中也是一样
第二步,新建控制器
namespace app\webapi\controller;
use think\facade\Db;
use think\facade\View;
use think\facade\Cache;
class Baidu
{
/**
* 发送短信验证码
* @param string $mobile 手机号
*/
public function sendsms()
{
die;
$mobile = '15678982250';//input('mobile');
$captcha = mt_rand(100000,999999);
$baidu_config=config('sms');
// /var_dump($baidu_config);die;
$data = [
"signatureId" => ', // 签名ID
"template" => ', // 模板ID
"mobile" => $mobile, // 手机号
"contentVar" => [ // 内容,对应模板中的参数
"id" => '1005678',
]
];
$ak = $baidu_config['baidu_sms']['api_key'];//'你的 AccessKey';
$sk = $baidu_config['baidu_sms']['secret_key'];//'你的 SecretKey';
$res = $this->getBaiduApi($data,$ak,$sk);
echo json_encode($res,320);
if($res['code'] == 1000){
jsonReturn(1,'发送成功');
}
jsonReturn(0,$res['message']);
}
/**
* 接收处理
* @param $data array 短信数据
* @param $ak string AccessKey
* @param $sk string SecretKey
* @return false|mixed
*/
public function getBaiduApi($data,$ak,$sk)
{
$host = 'smsv3.bj.baidubce.com'; // 服务 域名 (北京 smsv3.bj.baidubce.com , 苏州 smsv3.su.baidubce.com)
$uri = '/api/v3/sendSms'; // 接口路径
$startUtc = gmdate("Y-m-d\TH:i:s\Z"); // utc 时间戳
$Authorization = $this->getBaiDuAuthorization($startUtc,$uri,$host,$ak,$sk);
$url = "https://".$host.$uri;
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$header = [
'Authorization:'.$Authorization,
'Host:'.$host,
'content-type:application/json',
'x-bce-date:'.$startUtc,
'Content-Length:'.strlen($data)
];
return $this->getBaiDuPostCurl($url,$data,$header);
}
/**
* 生成签名 认证字符串
* ps: 可使用百度云签名计算工具进行校验
* @param [string] startUtc url
* @param [string] canonicalUri 请求数据
* @param [string] host 请求头
* @param [string] ak AccessKey
* @param [string] sk SecretKey
*/
function getBaiDuAuthorization($startUtc,$canonicalUri,$host,$ak,$sk)
{
// 签名有效期 (秒)
$validity = '1800';
// 代签名头域
$signedHeaders = 'host;x-bce-date';
// Host
$Host = "host:".$host;
// $startUtc = '2023-07-28T10:02:36Z'; // utc 时间
$BceDate = "x-bce-date:".UrlEncode($startUtc);
// http 方法
$httpRequestMethod = "POST";
$canonicalCan = '';
// 1: 规范化请求和前缀字符串
// 规范化请求 由HTTP请求信息规范化后生成
$canonicalRequest = $httpRequestMethod."\n" .$canonicalUri."\n" .$canonicalCan."\n" .$Host."\n" .$BceDate;
// 前缀字符串 由除sk字段外的签名信息生成
$authStringPrefix = "bce-auth-v1/".$ak."/".$startUtc."/".$validity;
// 2: 派生签名密钥 signingKey signingKey = HMAC-SHA-256-HEX("sk", authStringPrefix)
$signingKey = bin2hex(hash_hmac('sha256', $authStringPrefix,$sk, true));
// 3: 签名摘要 signature signature = HMAC-SHA-256-HEX(signingKey, canonicalRequest)
$signature = bin2hex(hash_hmac('sha256', $canonicalRequest,$signingKey, true));
// 4: 认证字符串 authorization Authorization = authStringPrefix/signedHeaders/signature
$Authorization = $authStringPrefix.'/'.$signedHeaders.'/'.$signature;
// var_dump($startUtc);
// var_dump($authStringPrefix);
// var_dump($canonicalRequest);
// var_dump($signingKey);
// var_dump($signature);
// var_dump($Authorization);exit;
return $Authorization;
}
/**
* post 请求
* @param [string] url url
* @param [string] data 请求数据
* @param [string] header 请求头
*/
function getBaiDuPostCurl($url,$data=null,$header=null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ( !empty($header) ) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$httpCode = curl_exec($ch);
$errorCode = curl_errno($ch);
curl_close($ch);
if(0 !== $errorCode) {
return false;
}
return json_decode($httpCode,true);
}
}