19
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LaravelでAWS SNSを利用してSMSを送る

Posted at

Laravelのインストール

$ laravel new LaravelSns
$ cd LaravelSns
$ php artisan --version
Laravel Framework 5.6.25

git の設定

$ git init
$ git add .
$ git commit -m 'init'

aws/aws-sdk-php-laravelのインストール・設定

$ composer require aws/aws-sdk-php-laravel
config/app.php
'providers' => [
  //...
  /*
  * Package Service Providers...
  */
+  Aws\Laravel\AwsServiceProvider::class,
],

設定ファイルを生成

$ php artisan vendor:publish  --provider="Aws\Laravel\AwsServiceProvider"
config/aws.php
+ 'credentials' => [
+   'key'    => env('AWS_ACCESS_KEY_ID'),
+   'secret' => env('AWS_SECRET_ACCESS_KEY'),
+ ],
'region' => env('AWS_REGION', 'us-east-1'),
'version' => 'latest',
'ua_append' => [
  'L5MOD/' . AwsServiceProvider::VERSION,
],

.envファイルにそれぞれ設定する

.env
+ AWS_ACCESS_KEY_ID=
+ AWS_SECRET_ACCESS_KEY=
+ AWS_REGION=

SMSを送る

コマンドを作成

$ php artisan make:command SendSms
app/Console/Commands/SendSms.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;

class SendSms extends Command
{
    protected $signature = 'send:sms';
    protected $description = 'SMS送信のテスト';
    public function handle()
    {
        $sns = App::make('aws')->createClient('sns');

        $sns->publish([
            'Message' => 'こんにちは',
            'PhoneNumber' => '+810000000000' // 電話番号を入れる
        ]);

    }
}

コマンドを実行

$ php artisan send:sms
19
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?