配置短信服務php
基於Laravel框架的使用方法laravel
安裝api
composer require mrgoon/aliyun-sms dev-masterapp
在config/app.php中添加以下代碼composer
'providers' => [
//......此處省略大量代碼
/**
* 阿里雲短信
*/
Mrgoon\AliSms\ServiceProvider::class,
],
//同時,能夠選擇性添加aliases
'aliases' => [
//......此處省略大量代碼
/**
* 阿里雲短信
*/
'AliSms'=>Mrgoon\AliSms\ServiceProvider::class,
],框架
添加系統服務ide
php artisan vendor:publish
#這裏要選擇對應的編號學習
上一步會新增config/aliyunsms.php 文件ui
'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