php天翼开放平台短信发送接口实现

作者:袖梨 2022-06-24
 代码如下 复制代码

//
date_default_timezone_set('PRC');
//获取access_token
$data = "app_id=x&app_secret=x&grant_type=client_credentials";
$ch = curl_init("https://oauth.api.189.cn/emp/oauth2/v2/access_token");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);//使用post提交数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//设置 post提交的数据
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 从证书中检查SSL加密算法是否存在

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$access_token = curl_exec($ch);
curl_close($ch);
$access_token = json_decode($access_token,true);
//获取短信信任码 www.111com.net
$timestamp = date('Y-m-d H:i:s');

$param['app_id'] = "app_id=x";
$param['access_token'] = "access_token=".$access_token['access_token'];
$param['timestamp'] = "timestamp=".$timestamp;
ksort($param);
$plaintext = implode("&",$param);
$sign = rawurlencode(base64_encode(hash_hmac('sha1',$plaintext,app_secret,true)));
//echo $sign;exit;
$code = file_get_contents("http://api.189.cn/v2/dm/randcode/token?app_id=x&access_token={$access_token['access_token']}×tamp=".$timestamp."&sign=".$sign);
$code = json_decode($code,true);
$code = $code['token'];
echo $code;
//下发验证码
unset($param,$plaintext,$sign);
$param['app_id'] = "app_id=x";
$param['access_token'] = "access_token=".$access_token['access_token'];
$param['token'] = "token=".$code;
$param['phone'] = "phone=15091421612";
$param['url'] = "url=http://wx.podapi.com/test.php";
$param['exp_time'] = "exp_time=2";
$param['timestamp'] = "timestamp=".$timestamp;
ksort($param);
$plaintext = implode("&",$param);
$sign = rawurlencode(base64_encode(hash_hmac('sha1',$plaintext,'xx',true)));

$data = "app_id=x&access_token={$access_token['access_token']}&token={$code}&phone=15091421612&url=http://wx.podapi.com/test.php&exp_time=2×tamp={$timestamp}&sign=".$sign;
$ch = curl_init("http://api.189.cn/v2/dm/randcode/send");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);//使用post提交数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);//设置 post提交的数据
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// 从证书中检查SSL www.111com.net 加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$state = curl_exec($ch);
//var_dump(curl_getinfo($ch));
curl_close($ch);
//echo $state;

相关文章

精彩推荐