php天翼开放平台短信发送接口实现方法
网络编程 2021-07-05 09:50www.168986.cn编程入门
这篇文章主要介绍了php天翼开放平台短信发送接口实现方法,是针对短信接口的经典应用,非常具有实用价值,需要的朋友可以参考下
本文实例讲述了php天翼开放平台短信发送接口实现方法。分享给大家供大家参考。具体分析如下
临时性需求,研究了一下天翼开发平台的东西,用来发送验证码还是不错的,每日限额不多,所以很鸡肋,保证100%到达 买的话还是蛮贵的,代码没有做任何优化处理,只是测试是否可以实现接口,用的同学记得完善代码,刚写完老大又说是鸡肋的东西,不用了,代码放在博客记录下,代码如下:
代码如下:
<?php
//
date_default_timezone_set('PRC');
//获取aess_token
$data = "app_id=x&app_secret=x&grant_type=client_credentials";
$ch = curl_init("https://oauth.api.189./emp/oauth2/v2/aess_token");
curl_set($ch,CURLOPT_RETURNTRANSFER,1);
curl_set($ch,CURLOPT_POST,1);//使用post提交数据
curl_set($ch,CURLOPT_POSTFIELDS,$data);//设置 post提交的数据
curl_set($ch, CURLOPT_SSL_VERIFYPEER, false);
// 从证书中检查SSL加密算法是否存在
curl_set($ch, CURLOPT_SSL_VERIFYHOST, false);
$aess_token = curl_exec($ch);
curl_close($ch);
$aess_token = json_decode($aess_token,true);
//获取短信信任码
$timestamp = date('Y-m-d H:i:s');
$param['app_id'] = "app_id=x";
$param['aess_token'] = "aess_token=".$aess_token['aess_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./v2/dm/randcode/token?app_id=x&aess_token={$aess_token['aess_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['aess_token'] = "aess_token=".$aess_token['aess_token'];
$param['token'] = "token=".$code;
$param['phone'] = "phone=15091421612";
$param['url'] = "url=http://wx.podapi./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&aess_token={$aess_token['aess_token']}&token={$code}&phone=15091421612&url=http://wx.podapi./test.php&exp_time=2×tamp={$timestamp}&sign=".$sign;
$ch = curl_init("http://api.189./v2/dm/randcode/send");
curl_set($ch,CURLOPT_RETURNTRANSFER,1);
curl_set($ch,CURLOPT_POST,1);//使用post提交数据
curl_set($ch,CURLOPT_POSTFIELDS,$data);//设置 post提交的数据
curl_set($ch, CURLOPT_SSL_VERIFYPEER, false);
// 从证书中检查SSL 加密算法是否存在
curl_set($ch, CURLOPT_SSL_VERIFYHOST, false);
$state = curl_exec($ch);
//var_dump(curl_getinfo($ch));
curl_close($ch);
//echo $state;
?>
//
date_default_timezone_set('PRC');
//获取aess_token
$data = "app_id=x&app_secret=x&grant_type=client_credentials";
$ch = curl_init("https://oauth.api.189./emp/oauth2/v2/aess_token");
curl_set($ch,CURLOPT_RETURNTRANSFER,1);
curl_set($ch,CURLOPT_POST,1);//使用post提交数据
curl_set($ch,CURLOPT_POSTFIELDS,$data);//设置 post提交的数据
curl_set($ch, CURLOPT_SSL_VERIFYPEER, false);
// 从证书中检查SSL加密算法是否存在
curl_set($ch, CURLOPT_SSL_VERIFYHOST, false);
$aess_token = curl_exec($ch);
curl_close($ch);
$aess_token = json_decode($aess_token,true);
//获取短信信任码
$timestamp = date('Y-m-d H:i:s');
$param['app_id'] = "app_id=x";
$param['aess_token'] = "aess_token=".$aess_token['aess_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./v2/dm/randcode/token?app_id=x&aess_token={$aess_token['aess_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['aess_token'] = "aess_token=".$aess_token['aess_token'];
$param['token'] = "token=".$code;
$param['phone'] = "phone=15091421612";
$param['url'] = "url=http://wx.podapi./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&aess_token={$aess_token['aess_token']}&token={$code}&phone=15091421612&url=http://wx.podapi./test.php&exp_time=2×tamp={$timestamp}&sign=".$sign;
$ch = curl_init("http://api.189./v2/dm/randcode/send");
curl_set($ch,CURLOPT_RETURNTRANSFER,1);
curl_set($ch,CURLOPT_POST,1);//使用post提交数据
curl_set($ch,CURLOPT_POSTFIELDS,$data);//设置 post提交的数据
curl_set($ch, CURLOPT_SSL_VERIFYPEER, false);
// 从证书中检查SSL 加密算法是否存在
curl_set($ch, CURLOPT_SSL_VERIFYHOST, false);
$state = curl_exec($ch);
//var_dump(curl_getinfo($ch));
curl_close($ch);
//echo $state;
?>
希望本文所述对大家的php程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程