阿里云发送短信

admin1年前Laravel634

public function sms()
{

   $moile = request()->input('mobile');
   if (!$moile) {
       return $this->error('请输入手机号');
   }
   $info = Db::table('code')->insert(['phone' => $moile, 'code' => 123456, 'status' => '0', 'time' => time()]);

   return $this->success('发送成功');
   exit;

   $code = mt_rand(100000, 999999);
   $templateParam = array(
       'code' => $code // 模板中变量的值
   );

   $params = array(
       'PhoneNumbers' => $moile,
       'SignName' => $this->signName,
       'TemplateCode' => $this->templateCode,
       'TemplateParam' => json_encode($templateParam),
   );

   $params['RegionId'] = 'cn-hangzhou';
   $params['Action'] = 'SendSms';
   $params['Version'] = '2017-05-25';
   $params['Format'] = 'JSON';
   $params['SignatureMethod'] = 'HMAC-SHA1';
   $params['SignatureVersion'] = '1.0';
   $params['SignatureNonce'] = uniqid();
   $params['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
   $params['AccessKeyId'] = $this->accessKeyId;
   ksort($params);
   $canonicalizedQueryString = '';
   foreach ($params as $key => $value) {
       $canonicalizedQueryString .= '&' . $this->encode($key) . '=' . $this->encode($value);
   }
   $stringToSign = 'GET&%2F&' . $this->encode(substr($canonicalizedQueryString, 1));
   $signature = base64_encode(hash_hmac('sha1', $stringToSign, $this->accessKeySecret . '&', true));
   $requestUrl = 'http://dysmsapi.aliyuncs.com/?Signature=' . $this->encode($signature) . $canonicalizedQueryString;

   // 发送 HTTP 请求并处理响应
   $response = file_get_contents($requestUrl);
   $responseObject = json_decode($response);

   if ($responseObject->Code == 'OK') {
       Db::beginTransaction();
       $info = Db::table('code')->insert(['phone' => $moile, 'code' => $code, 'status' => '0', 'time' => time()]);
       if ($info) {
           Db::commit();
           return $this->success('发送成功');
       } else {
           Db::rollBack();
           return $this->error("发送失败");
       }
   } else {
       return $this->error($responseObject->Message);
   }
}


protected function encode($str)
{
   $res = urlencode($str);
   $res = str_replace('+', '%20', $res);
   $res = str_replace('*', '%2A', $res);
   $res = str_replace('%7E', '~', $res);
   return $res;
}


相关文章

excel 导出

header("Content-Type: application/vnd.ms-excel; name='excel'"); header("Content-Disposition:atta...

修改密码

/** * 修改密码 */publicfunction setpass(){    $type = request()->input('type');// 1...

手机号替换

 $vv = substr_replace($vv, '****', 3, 4) ....

获取域名

/** * 获取域名 */public function domain(){    return request()->getSchemeAndHttpHost();}...

微信退款v3

/** * 微信 退款 操作 */ public function refund($out_trade_no, $refund_money) { $time = time();...

压缩图片

public static function size($id, $img) { try { list($width, $height, $img_type, $attr) = @getimages...