PHP开发调用阿里云短信验证码的代码-直接可用
- 1:最低要求 PHP 5.6
- 2:安装了composer
- 3:阿里云composer镜像地址命令(如果设置过就不需要):
- composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
- 4:安装 SDK 核心库 OpenAPI : Alibaba Cloud SDK for PHP 作为依赖项:
- composer require alibabacloud/darabonba-openapi
- 5:阿里云短信SDK安装包命令(官方地址:https://next.api.aliyun.com/api-tools/sdk/Dysmsapi):
- composer require alibabacloud/dysmsapi-20170525 2.0.8
<?php
// This file is auto-generated, don't edit it. Thanks.
namespace lib;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use Darabonba\OpenApi\Models\Config;
class aliyunSms
{
private static $accessKeyId = 'LTAI5t7AC3RH3333pZTDCaA3';//accessKeyId
private static $accessKeySecret = 'ihDUcyqNZvNYXASfLtijI33333NSk';//accessKeySecret
private static $signName = 'xxxx技有限公司';//签名
private static $templateCode = 'SMS_228533331';//模板代码
/**
* 使用AK&SK初始化账号Client
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Dysmsapi Client
*/
private static function createClient($accessKeyId, $accessKeySecret)
{
$config = new Config([
// 您的AccessKey ID
"accessKeyId" => $accessKeyId,
// 您的AccessKey Secret
"accessKeySecret" => $accessKeySecret
]);
// 访问的域名
$config->endpoint = "dysmsapi.aliyuncs.com";
return new Dysmsapi($config);
}
/**
* @param string $phoneNumbers 手机号
* @param string $code 验证码
* @return void
*/
// public static function main($args)
private static function main($phoneNumbers, $code)
{
$client = self::createClient(self::$accessKeyId, self::$accessKeySecret);
$sendSmsRequest = new SendSmsRequest([
"templateParam" => "{\"code\":\"{$code}\"}",
"phoneNumbers" => "{$phoneNumbers}",
"signName" => self::$signName,
"templateCode" => self::$templateCode
]);
$ali_res = $client->sendSms($sendSmsRequest);
if ($ali_res->body->code == 'OK' && $ali_res->body->bizId != NULL) {
return true;
}
switch ($ali_res->body->code) {
case 'isv.BUSINESS_LIMIT_CONTROL':
exception('短信发送频繁,请稍候再试');//tp的抛出错误,换成你自己的报错
break;
case 'isv.TEMPLATE_PARAMS_ILLEGAL':
exception('短信验证码不符合变量规范');//tp的抛出错误,换成你自己的报错
break;
case 'isv.MOBILE_NUMBER_ILLEGAL':
exception('手机号不正确,无法发送短信');//tp的抛出错误,换成你自己的报错
break;
}
//少见的错误,记录下来
//log_err($ali_res->body, '发送短信发生错误', 'ali_sms');//换成你的
exception($ali_res->body->message);//tp的抛出错误,换成你自己的报错
// 以下是阿里云短信正确和失败返回的数据,以作参考
// 失败演示返回数据
/* object(AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsResponseBody)#81 (6) {
["bizId"] => NULL
["code"] => string(24) "isv.SMS_TEMPLATE_ILLEGAL"
["message"] => string(38) "模板不合法(不存在或被拉黑)"
["requestId"] => string(36) "21A90D61-2D5E-533D-BFE7-9D16F8312A0E"
["_name":protected] => array(4) {
["bizId"] => string(5) "BizId"
["code"] => string(4) "Code"
["message"] => string(7) "Message"
["requestId"] => string(9) "RequestId"
}
["_required":protected] => array(0) {
}
}
*/
// 成功返回数据演示
/* object(AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsResponseBody)#81 (6) {
["bizId"] => string(20) "839015438514162136^0"
["code"] => string(2) "OK"
["message"] => string(2) "OK"
["requestId"] => string(36) "EA37C2B7-E427-59F8-8B7C-06AD846A5439"
["_name":protected] => array(4) {
["bizId"] => string(5) "BizId"
["code"] => string(4) "Code"
["message"] => string(7) "Message"
["requestId"] => string(9) "RequestId"
}
["_required":protected] => array(0) {
}
}*/
}
//发短信
public static function sendSms($phoneNumbers, $code)
{
$res = self::main($phoneNumbers, $code);
return $res;
}
}
此代码只需要修改命名空间和阿里云accessKeyId等相关信息,即可使用~
exception是TP的错误异常抛出,我是做了全局的异常托管,并且在所有报错的地方调用此方法就能终端代码,报出错误,你只需要换成你的中断代码返回错误即可。
调用只需要 类名::sendSms(手机号,验证码号码); :
\lib\aliyunSms::sendSms($mobile, $code);
本文来自博客园,作者:wekyun,转载请注明原文链接:https://www.cnblogs.com/wekyun/articles/15636708.html