腾讯云发送短信
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Support\Helper;
use Illuminate\Support\Facades\Validator;
use phpDocumentor\Reflection\Types\True_;
class CodeController extends ApiController
{
// 需要登录
protected $Need = [];
private $url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";
private $appid = ;
private $appkey = "";
private $smsSign = "";
private $templateId = ;
public function codes()
{
$phone = request()->input('phone');
if (empty($phone)) {
return $this->error('请输入手机号');
}
$num = Db::table('member')->where(array('phone' => $phone))->first();
if ($num) {
return $this->error('账号已存在,请登录');
}
$code = mt_rand(100000, 999999);
$params = [$code];
if(request()->getClientIp()=='58.57.39.82'){
$code='123456';
$info = Db::table('code')->insert(['phone' => $phone, 'code' => $code, 'status' => '0', 'time' => time()]);
return $this->success('ok', '发送成功');
exit;
}
$result = $this->sendWithParam("86", $phone, $this->templateId, $params, $this->smsSign, "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
$data = json_decode($result, 1);
if ($data['errmsg'] == 'OK') {
$info = Db::table('code')->insert(['phone' => $phone, 'code' => $code, 'status' => '0', 'time' => time()]);
if (true) {
return $this->success('ok', '发送成功');
} else {
return $this->error('发送失败', '发送失败');
}
} else {
return $this->error($data['errmsg'], $data['errmsg']);
}
}
public function code()
{
$phone = request()->input('phone');
if (empty($phone)) {
return $this->error('请输入手机号');
}
if(request()->getClientIp()=='58.57.39.82'){
$code='123456';
$info = Db::table('code')->insert(['phone' => $phone, 'code' => $code, 'status' => '0', 'time' => time()]);
return $this->success('ok', '发送成功');
exit;
}
$code = mt_rand(100000, 999999);
$params = [$code];
$result = $this->sendWithParam("86", $phone, $this->templateId, $params, $this->smsSign, "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
$data = json_decode($result, 1);
if ($data['errmsg'] == 'OK') {
$info = Db::table('code')->insert(['phone' => $phone, 'code' => $code, 'status' => '0', 'time' => time()]);
if (true) {
return $this->success('ok', '发送成功');
} else {
return $this->error('发送失败', '发送失败');
}
} else {
return $this->error($data['errmsg'], $data['errmsg']);
}
}
public function sendWithParam($nationCode, $phoneNumber, $templId = 0, $params,
$sign = "", $extend = "", $ext = "")
{
$random = $this->getRandom();
$curTime = time();
$wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random;
// 按照协议组织 post 包体
$data = new \stdClass();
$tel = new \stdClass();
$tel->nationcode = "" . $nationCode;
$tel->mobile = "" . $phoneNumber;
$data->tel = $tel;
$data->sig = $this->calculateSigForTempl($this->appkey, $random,
$curTime, $phoneNumber);
$data->tpl_id = $templId;
$data->params = $params;
$data->sign = $sign;
$data->time = $curTime;
$data->extend = $extend;
$data->ext = $ext;
return $this->sendCurlPost($wholeUrl, $data);
}
public function getRandom()
{
return rand(100000, 999999);
}
/**
* 生成签名
*
* @param string $appkey sdkappid对应的appkey
* @param string $random 随机正整数
* @param string $curTime 当前时间
* @param array $phoneNumber 手机号码
* @return string 签名结果
*/
public function calculateSigForTempl($appkey, $random, $curTime, $phoneNumber)
{
$phoneNumbers = array($phoneNumber);
return $this->calculateSigForTemplAndPhoneNumbers($appkey, $random,
$curTime, $phoneNumbers);
}
/**
* 生成签名
*
* @param string $appkey sdkappid对应的appkey
* @param string $random 随机正整数
* @param string $curTime 当前时间
* @param array $phoneNumbers 手机号码
* @return string 签名结果
*/
public function calculateSigForTemplAndPhoneNumbers($appkey, $random,
$curTime, $phoneNumbers)
{
$phoneNumbersString = $phoneNumbers[0];
for ($i = 1; $i < count($phoneNumbers); $i++) {
$phoneNumbersString .= ("," . $phoneNumbers[$i]);
}
return hash("sha256", "appkey=" . $appkey . "&random=" . $random
. "&time=" . $curTime . "&mobile=" . $phoneNumbersString);
}
/**
* 发送请求
*
* @param string $url 请求地址
* @param array $dataObj 请求内容
* @return string 应答json字符串
*/
public function sendCurlPost($url, $dataObj)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$ret = curl_exec($curl);
if (false == $ret) {
// curl_exec failed
$result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
} else {
$rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (200 != $rsp) {
$result = "{ \"result\":" . -1 . ",\"errmsg\":\"" . $rsp
. " " . curl_error($curl) . "\"}";
} else {
$result = $ret;
}
}
curl_close($curl);
return $result;
}
}