├── .gitignore ├── src ├── config.php ├── ServiceProvider.php └── AliSms.php ├── composer.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | .idea/* 3 | -------------------------------------------------------------------------------- /src/config.php: -------------------------------------------------------------------------------- 1 | env('ALIYUN_SMS_AK'), // accessKey 6 | 'access_secret' => env('ALIYUN_SMS_AS'), // accessSecret 7 | 'sign_name' => env('ALIYUN_SMS_SIGN_NAME'), // 签名 8 | ]; 9 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mrgoon/aliyun-sms", 3 | "description": "阿里云短信服务php调用代码", 4 | "keywords": ["sdk", "aliyun", "aliyun-sms"], 5 | "version": "2.0.1", 6 | "require": { 7 | "php": ">5.3", 8 | "mrgoon/aliyun-sms-sdk": ">=1.1.2" 9 | }, 10 | "license": "MIT", 11 | "autoload": { 12 | "psr-4": {"Mrgoon\\AliSms\\": "src/"} 13 | }, 14 | "authors": [ 15 | { 16 | "name": "mrgoon", 17 | "email": "mrgoon@foxmail.com" 18 | } 19 | ], 20 | "minimum-stability": "dev" 21 | } 22 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 20 | __DIR__.'/config.php' => config_path('aliyunsms.php'), 21 | ], 'config'); 22 | 23 | } 24 | 25 | public function register() 26 | { 27 | 28 | $this->mergeConfigFrom(__DIR__.'/config.php', 'aliyunsms'); 29 | 30 | $this->app->bind(AliSms::class, function() { 31 | return new AliSms(); 32 | }); 33 | } 34 | 35 | protected function configPath() 36 | { 37 | return __DIR__ . '/config.php'; 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 介绍 2 | 阿里大于关闭了新用户渠道,迁移到阿里云短信服务上,然而阿里云短信服务在5月底更新了api。 3 | 在github上找了一下,轮子都是基于2016年的接口,非常难受。 4 | 自己造了一个轮子,亲测可用。 5 | 6 | * 安装 7 | ` composer require mrgoon/aliyun-sms dev-master ` 8 | 9 | ## 基于laravel框架的使用方法 10 | 11 | * 加载 12 | 在config/app的providers中添加 13 | ` Mrgoon\AliSms\ServiceProvider::class ` 14 | 15 | 同时,可以选择性添加aliases 16 | 17 | 控制台运行: 18 | ` php artisan vendor:publish ` 19 | 20 | 根据新增的` aliyunsms.php ` 文件,在.env文件中添加环境变量: 21 | ``` 22 | ALIYUN_SMS_AK=your access key 23 | ALIYUN_SMS_AS=your secret key 24 | ALIYUN_SMS_SIGN_NAME=sign name 25 | ``` 26 | 27 | * 使用 28 | ``` 29 | $aliSms = new AliSms(); 30 | $response = $aliSms->sendSms('phone number', 'SMS_code', ['name'=> 'value in your template']); 31 | //dump($response); 32 | ``` 33 | 34 | ## 非laravel框架的使用方法 35 | 36 | * 加载方式通过composer,不变 37 | * 使用样例代码如下: 38 | 39 | ``` 40 | $config = [ 41 | 'access_key' => 'your access key', 42 | 'access_secret' => 'your access secret', 43 | 'sign_name' => 'your sign name', 44 | ]; 45 | 46 | $aliSms = new Mrgoon\AliSms\AliSms(); 47 | $response = $sms->sendSms('phone number', 'tempplate code', ['name'=> 'value in your template'], $config); 48 | ``` 49 | -------------------------------------------------------------------------------- /src/AliSms.php: -------------------------------------------------------------------------------- 1 | setPhoneNumbers($to); 42 | //必填-短信签名 43 | $request->setSignName($signName); 44 | //必填-短信模板Code 45 | $request->setTemplateCode($template_code); 46 | //选填-假如模板中存在变量需要替换则为必填(JSON格式) 47 | if ($data) { 48 | $request->setTemplateParam(json_encode($data)); 49 | } 50 | 51 | //选填-发送短信流水号 52 | if ($outId) { 53 | $request->setOutId($outId); 54 | } 55 | 56 | //发起访问请求 57 | return $acsClient->getAcsResponse($request); 58 | } 59 | } 60 | 61 | 62 | 63 | //function querySendDetails() { 64 | // 65 | // //此处需要替换成自己的AK信息 66 | // $accessKeyId = "yourAccessKeyId"; 67 | // $accessKeySecret = "yourAccessKeySecret"; 68 | // //短信API产品名 69 | // $product = "Dysmsapi"; 70 | // //短信API产品域名 71 | // $domain = "dysmsapi.aliyuncs.com"; 72 | // //暂时不支持多Region 73 | // $region = "cn-hangzhou"; 74 | // 75 | // //初始化访问的acsCleint 76 | // $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret); 77 | // DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain); 78 | // $acsClient= new DefaultAcsClient($profile); 79 | // 80 | // $request = new Dysmsapi\Request\V20170525\QuerySendDetailsRequest(); 81 | // //必填-短信接收号码 82 | // $request->setPhoneNumber("15000000000"); 83 | // //选填-短信发送流水号 84 | // $request->setBizId("abcdefgh"); 85 | // //必填-短信发送日期,支持近30天记录查询,格式yyyyMMdd 86 | // $request->setSendDate("20170525"); 87 | // //必填-分页大小 88 | // $request->setPageSize(10); 89 | // //必填-当前页码 90 | // $request->setContent(1); 91 | // 92 | // //发起访问请求 93 | // $acsResponse = $acsClient->getAcsResponse($request); 94 | // var_dump($acsResponse); 95 | // 96 | //} 97 | 98 | 99 | ?> --------------------------------------------------------------------------------