基於 ThinkPHP 呼叫阿里雲簡訊介面

wml_macho發表於2020-06-04

1.composer 下載 阿里雲簡訊類

composer require alibabacloud/client

2.呼叫阿里雲簡訊程式碼

/**
 * 阿里雲簡訊驗證
 * @param $mobile
 * @param $code
 * @param $tempId
 * @return array
 * @throws ClientException
 *
 * keyID XXXXXXXXXXXXXXXXXXXXX
 * keySecret XXXXXXXXXXXXXXXXXX
 * tempId SMS_xxxxxxx
 *
 */
function smsVerify($mobile, $code, $tempId)
{
    header("Access-Control-Allow-Origin:*");
    header('Access-Control-Allow-Methods:POST');
    header('Access-Control-Allow-Headers:x-requested-with, content-type');
    AlibabaCloud::accessKeyClient("keyID", "keySecret")
        ->regionId('cn-hangzhou') //replace regionId as you need(這個地方是發簡訊的節點,預設即可,或者換成你想要的)
        ->asGlobalClient();
    $data = [];
    try {
        $result = AlibabaCloud::rpcRequest()
            ->product('Dysmsapi')
            //->scheme('https') //https | http(如果域名是https,這裡記得開啟)
            ->version('2017-05-25')
            ->action('SendSms')
            ->method('POST')
            ->options([
                'query'                 => [
                    'PhoneNumbers'      => $mobile,
                    'SignName'          => "xx商城", //簡訊簽名
                    'TemplateCode'      => $tempId,
                    'TemplateParam'     => json_encode(['code'=>$code]),
                ],
            ])
            ->request();
        $res    = $result->toArray();
        if($res['Code'] == 'OK'){
            $data['status'] = 1;
            $data['info']   = $res['Message'];
        }else{
            $data['status'] = 0;
            $data['info']   = $res['Message'];
        }
        return $data;
    } catch (ClientException $e) {
        $data['status'] = 0;
        $data['info']   = $e->getErrorMessage();
        return $data;
    } catch (ServerException $e) {
        $data['status'] = 0;
        $data['info']   = $e->getErrorMessage();
        return $data;
    }
}
  • 3專案中呼叫處理

      /**
       * 獲取驗證碼
       * @return false|string
       * @throws \AlibabaCloud\Client\Exception\ClientException
       * @throws \think\db\exception\DataNotFoundException
       * @throws \think\db\exception\ModelNotFoundException
       * @throws \think\exception\DbException
       */
      public function getCode(){
          $mobile = $this->request->mobile;
          $state = $this->request->state ?? 0; //state == 0 註冊 state == 1忘記密碼 修改密碼
          if(empty($mobile)) return errorMsg('-10012');
          if($state == 0){
              $data = Commercial::where('mobile',$mobile)->find();
              if(!empty($data)) return errorMsg('-10011');
          }
          $code = Redis::getInstance()->get($mobile);
          if(isset($code) && !empty($code)) return errorMsg('-10016');
          $rand = rand(10000,99999);
          Redis::getInstance()->set($mobile,$rand,5*60); //調整驗證碼 快取時間
          // ****************************呼叫 以上邏輯不用管 根據自己需求********************** 
          $res    = smsVerify($mobile, $rand, 'SMS_XXXX'); //模板ID SMS_XXXXX
          // ****************************呼叫 end********************** 
          if($res['status'] != 1) return errorMsg("-10017");
          return successMsg([]);
      }
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章