<?php
require_once(‘引入你自己的’);
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\Tea\Tea;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Console\Console;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
class SmsSingleSender {
/**
* 使用AK&SK初始化账号Client
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Dysmsapi Client
*/
public static function createClient(){
$config = new Config([
// 您的AccessKey ID
"accessKeyId" => "你自己的ID",
// 您的AccessKey Secret
"accessKeySecret" => "你自己的KEY"
]);
// 访问的域名
$config->endpoint = "dysmsapi.aliyuncs.com";
return new Dysmsapi($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($mobile,$code){
$client = self::createClient();
$templ = ['code'=>$code];
$sendSmsRequest = new SendSmsRequest([
"phoneNumbers" => $mobile,
"signName" => "你自己的",
"templateCode" => "你自己的",
"templateParam" => json_encode($templ)
]);
$resp = $client->sendSms($sendSmsRequest);
if ($resp->body->code =="OK" && $resp->body->message =="OK" ){
return true;
}
return false;
}
}
$path = DIR . \DIRECTORY_SEPARATOR . ‘..’ . \DIRECTORY_SEPARATOR . ‘vendor’ . \DIRECTORY_SEPARATOR . ‘autoload.php’;
if (file_exists($path)) {
require_once $path;
}
//接收参数
public function index(){
$mobile = request()->get('mobile/s','');
if (empty($mobile)) throw new ErrorException(['msg'=>"手机号不能为空"]);
$code = rand(111111,999999);
$res=(new \app\api\service\SendSms())->send($mobile,$code);
$res = json_decode($res,true);
if ($res['SendStatusSet'][0]['Code'] =="Ok") {
cache($mobile,$code,300);
return json(['code'=>200,'msg'=>"发送成功"]);
}
return json(['code'=>201,'msg'=>"ok"]);
}