在 Laravel 5.5+ 应用中配置使用阿里云提供的短信服务

配置短信服务

基于Laravel框架的使用方法

    安装

composer require mrgoon/aliyun-sms dev-master

    在config/app.php中添加如下代码

    'providers' => [
     
        //......此处省略大量代码
     
        /**
         * 阿里云短信
         */
        Mrgoon\AliSms\ServiceProvider::class,
    ],
     
    //同时,可以选择性添加aliases
    'aliases' => [
     
        //......此处省略大量代码
     
        /**
         * 阿里云短信
         */
        'AliSms'=>Mrgoon\AliSms\ServiceProvider::class,
    ],

    添加系统服务

    php artisan vendor:publish
    #这里要选择对应的编号

    上一步会新增config/aliyunsms.php 文件

    'access_key'        => env('ALIYUN_ACCESSKEYID'), // accessKey
    'access_secret'     => env('ALIYUN_ACCESSKEYSECRET'), // accessSecret
    'sign_name'         => env('ALIYUN_SMS_SIGN_NAME'), // 签名

    在.env文件中添加环境变量:

    ALIYUN_ACCESSKEYID=your access key
    ALIYUN_ACCESSKEYSECRET=your secret key
    ALIYUN_SMS_SIGN_NAME=sign name

    使用

    <?php
    /**
     * Created by PhpStorm.
     * User: zoe
     * Date: 2018/7/18
     * Time: 下午4:56
     */
    namespace App\Http\Controllers\api;
     
    use App\Http\Controllers\Controller;
    use Mrgoon\AliSms\AliSms;
     
    class SendMsgController extends Controller {
        public function sendMsg() {
            $aliSms = new AliSms();
            $response = $aliSms->sendSms('12345678910', 'SMS_139970365', ['code'=> 'hehe', 'product' => 'hehe']);    //code不能少
            print_r($response);
        }
    }

对于laravel引入的扩展库,其命名空间是怎么确认的,还有待学习,哈哈

非laravel框架的使用方法

    加载方式通过composer,不变
    使用样例代码如下:

    $config = [
        'access_key' => 'your access key',
        'access_secret' => 'your access secret',
        'sign_name' => 'your sign name',
    ];
     
        $aliSms = new Mrgoon\AliSms\AliSms();
        $response = $sms->sendSms(
            'phone number',
            'tempplate code',
            ['name'=> 'value in your template'],
            $config
        );

转载地址:

https://blog.csdn.net/GorgeousChou/article/details/85677618

转载于:https://my.oschina.net/u/2444569/blog/3089874

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值