杨小杰Blog(Youngxj)提供免费教程下载和网站搭建技术教程,主要分享和发布网站源码,致力创造一个高质量网络资源教程的分享平台

腾讯云短信发送接口版

Young小杰2018-9-13 23:29 网站搭建(2)6608小标签: 原创 腾讯云 接口

要求:

如果需要使用短信业务,请先满足以下要求:

1、腾讯云账号一只

2、个人及个人以上实名认证

3、网站域名一只(备案:个人备案上传后台备案信息截图、企业备案需要上传营业执照)

ps:第3非必要,视签名类型而定。腾讯云默认赠送100条免费短信。

操作流程:

1、进入腾讯云短信添加应用

添加应用.png

2、创建短信签名

创建短信签名.png

    这项需要上传一些特定的资料,视签名类型而定。坐等审核(几分钟就能出结果),审核完毕继续下一步

3、创建正文模版

创建正文模版.png

    按照划线填写即可,当然你也可以填自己的模版

4、等以上两项都审核完毕之后就可以进行短信发信了。

php发信demo

<?php
/**
 * 腾讯云接口指定模板单发短信demo
 * @author Youngxj <blog@youngxj.cn>
 * @var 1.0
 * @time  2018年9月14日 00:03:49
 */
$r = rand('100000','999999');//官方要求的随机数
$t = time();//官方要求的时间戳

$sdkAppid = '1400151023'; //填写自己的Appid即可
$strAppKey = "5f03a35d00ee52a21327ab048186a2c4"; //sdkappid 对应的 appkey,需要业务方高度保密
$strMobile = "13800138000"; //收件人手机号
$strRand = $r; //url 中的 random 字段的值
$strTime = $t; //UNIX 时间戳

$sig = hash('sha256','appkey='.$strAppKey.'&random='.$r.'&time='.$t.'&mobile='.$strMobile);//官方要求sig算法

$MsgContent = '12345';//这里应该填写短信正文模版中{1}的内容
$MsgTime = '5';//这里应该填写短信正文模版中{2}的内容
//如果还有更多参数请在$post_data['params']中添加
$MsgTpl = '19';//填写短信正文模版id

$post_data = array(
	"ext"=>"",//用户的 session 内容,腾讯 server 回包中会原样返回,可选字段,不需要就填空
	"extend"=>"",
	"params"=>array(
		$MsgContent,$MsgTime
	),
	"sig"=>$sig,
	"sign"=>"",
	"tel"=>array(
		"mobile"=>$strMobile,
		"nationcode"=>"86"//86为国内手机号前缀
	),
	"time"=>$t,
	"tpl_id"=>$MsgTpl
);

$parameters = json_encode($post_data);//官方要求数据json之后post

$url = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid='.$sdkAppid.'&random='.$r;

$response = get_curl($url,$parameters);

$responseData=json_decode($response,true);//发序列化为数组
$EchoMsgResult = $responseData['result'];//错误码,0 表示成功(计费依据),非 0 表示失败
$EchoMsgErrmsg = $responseData['errmsg'];//错误消息,result 非 0 时的具体错误信息
$EchoMsgExt = $responseData['ext'];//用户的 session 内容,腾讯 server 回包中会原样返回
$EchoMsgSid = $responseData['sid'];//本次发送标识 id,标识一次短信下发记录
$EchoMsgFee = $responseData['fee'];//短信计费的条数

if($EchoMsgResult==0){
	echo '发送成功'.$EchoMsgErrmsg;
}else{
	echo '发送失败'.$EchoMsgErrmsg;
}
/**
 * curl函数
 * @param  [type]  $url     [description]
 * @param  integer $post    [description]
 * @param  integer $referer [description]
 * @param  integer $cookie  [description]
 * @param  integer $header  [description]
 * @param  integer $ua      [description]
 * @param  integer $nobaody [description]
 * @return [type]           [description]
 */
function get_curl($url, $post=0, $referer=0, $cookie=0, $header=0, $ua=0, $nobaody=0){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	$httpheader[] = "Accept:application/json";
	$httpheader[] = "Accept-Encoding:gzip,deflate,sdch";
	$httpheader[] = "Accept-Language:zh-CN,zh;q=0.8";
	$httpheader[] = "Connection:close";
	curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
	if ($post) {
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	}
	if ($header) {
		curl_setopt($ch, CURLOPT_HEADER, true);
	}
	if ($cookie) {
		curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	}
	if($referer){
		if($referer==1){
			curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
		}else{
			curl_setopt($ch, CURLOPT_REFERER, $referer);
		}
	}
	if ($ua) {
		curl_setopt($ch, CURLOPT_USERAGENT, $ua);
	}
	else {
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0");
	}
	if ($nobaody) {
		curl_setopt($ch, CURLOPT_NOBODY, 1);
	}
	curl_setopt($ch, CURLOPT_TIMEOUT, 3);
	curl_setopt($ch, CURLOPT_ENCODING, "gzip");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$ret = curl_exec($ch);
	curl_close($ch);
	return $ret;
}
?>

请求参数

参数 必选 类型 描述
ext 否 string 用户的 session 内容,腾讯 server 回包中会原样返回,可选字段,不需要就填空
extend 否 string 短信码号扩展号,格式为纯数字串,其他格式无效。默认没有开通,开通请联系 腾讯云短信技术支持
params 是 array 模板参数,若模板没有参数,请提供为空数组
sig 是 string App 凭证,具体计算方式见下注
sign 否 string 短信签名,如果使用默认签名,该字段可缺省
tel 是 object 国际电话号码,格式依据 e.164 标准为: +[国家码][手机号] ,示例如:+8613711112222, 其中前面有一个 + 符号 ,86 为国家码,13711112222为手机号
time 是 number 请求发起时间,UNIX 时间戳(单位:秒),如果和系统时间相差超过 10 分钟则会返回失败
tpl_id 是 number 模板 ID,在控制台审核通过的模板 ID


响应参数

参数 必选 类型 描述
result 是 number 错误码,0 表示成功(计费依据),非 0 表示失败,参考 错误码
errmsg 是 string 错误消息,result 非 0 时的具体错误信息
ext 否 string 用户的 session 内容,腾讯 server 回包中会原样返回
fee 否 number 短信计费的条数,"fee" 字段计费说明
sid 否 string 本次发送标识 id,标识一次短信下发记录

本文最后更新于2018-9-13,已超过 1 年没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!

发表评论:

评论列表:

  • 这代码 搞亮 看的我眼睛啊。。

  • 汐岑君 Lv 1

    优秀

  • 手机扫描二维码
    阅读体验更佳