p_ysk/app/model/AlibabaSms.php

67 lines
1.8 KiB
PHP

<?php
namespace app\model;
use think\Exception;
use support\Log;
use think\Model;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
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 AlibabaSms extends Model
{
/**
* 使用AK&SK初始化账号Client
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Dysmsapi Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
$config->endpoint = "dysmsapi.aliyuncs.com";
return new Dysmsapi($config);
}
/**
* @param array $args
* @return void
*/
public static function main($args){
$client = self::createClient(config('cons.alibaba_sms.access_key_id'), config('cons.alibaba_sms.access_key_secret'));
$sendSmsRequest = new SendSmsRequest($args);
$runtime = new RuntimeOptions([]);
try {
// 复制代码运行请自行打印 API 的返回值
$res = $client->sendSmsWithOptions($sendSmsRequest, $runtime);
if($res->body->code == 'OK') {
return true;
}else {
return false;
}
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// 如有需要,请打印 error
Utils::assertAsString($error->message);
Log::info('短信发送错误--' . $error->message);
return false;
}
}
}