2023年的代码:
一、准备好AccessKey ID、AccessKey Secret
二、composer下载
composer require alibabacloud/dysmsapi-20170525 2.0.24
三、app\common\lib建立Alicms.php,代码如下:
<?php namespace app\common\lib; use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi; use \Exception; use AlibabaCloud\Tea\Exception\TeaError; use AlibabaCloud\Tea\Utils\Utils; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest; use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions; class Alicms { /** * 使用AK&SK初始化账号Client * @param string $accessKeyId * @param string $accessKeySecret * @return Dysmsapi Client */ public static function createClient($accessKeyId, $accessKeySecret){ $config = new Config([ // 必填,您的 AccessKey ID "accessKeyId" => 'LTAI5tLU8d1utb1amqqXGC', // 必填,您的 AccessKey Secret "accessKeySecret" => '3xnCK911hkEsFVlBtq7MxRtJho91y' ]); // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi $config->endpoint = "dysmsapi.aliyuncs.com"; return new Dysmsapi($config); } /** * @param string[] $args * @return void */ public static function main(){ // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/311677.html $client = self::createClient(getenv("LTAI5tLU8xsmqqXGC"), getenv('3xnttFVlBtq7MxRtwJho91y')); $sendSmsRequest = new SendSmsRequest([ "phoneNumbers" => "18315917852", "signName" => "点点商城", "templateCode" => "SMS_195720139", "templateParam" => "{\"code\":\"1634\"}" ]); $runtime = new RuntimeOptions([]); // true 忽略证书校验;false 设置证书校验 $runtime->ignoreSSL = true; try { // 复制代码运行请自行打印 API 的返回值 $res=$client->sendSmsWithOptions($sendSmsRequest, $runtime); } catch (Exception $error) { if (!($error instanceof TeaError)) { $error = new TeaError([], $error->getMessage(), $error->getCode(), $error); } // 如有需要,请打印 error Utils::assertAsString($error->message); } } } $path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php'; if (file_exists($path)) { require_once $path; }
四、控制器中调用,先use
use app\common\lib\Alicms;
$alibaba=new Alicms(); $res=$alibaba->main([]);
以上是基础代码,需要根据自身业务修改,ssl、随机数生成、存入缓存等。
2020年的代码:
去官网准备composer一把,结果又改了。调皮啊。
composer命令见上面官方文档
缓存不能用数字开头,比如电话号码直接存成缓存,即使已经转字符串,也不行的,至少在阿里云主机不行
来把代码,看看类库都变了……,也可以踹(try)一下子
declare(strict_types = 1); namespace app\common\lib; use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi; use Darabonba\OpenApi\Models\Config; use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest; use think\facade\Cache; use Redis; class Alicms{ public function index(string $phone){ if(empty($phone)){ return false; } $code=rand(1000,9999); Cache::store('redis')->set($phone,$code,90); $client = $this->createClient("LTAI5tHtHf6TgU7ZygkDn", "bPJIMPnjYn8K2KCmzSRrQtAjkf"); $sendSmsRequest = new SendSmsRequest([ "phoneNumbers" => $phone, "signName" => "XX育", "templateCode" => "SMS_21", "templateParam" => "{\"code\":$code}", ]); // 复制代码运行请自行打印 API 的返回值 $result=$client->sendSms($sendSmsRequest); return "OK"; } public function createClient($accessKeyId,$accessKeySecret){ $config = new Config([ // 您的AccessKey ID "accessKeyId" => $accessKeyId, // 您的AccessKey Secret "accessKeySecret" => $accessKeySecret ]); // 访问的域名 $config->endpoint = "dysmsapi.aliyuncs.com"; return new Dysmsapi($config); } }