kevupton/twilavel

PHP Twilio for Laravel 和 Lumen

安装量: 1,063

依赖关系: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 1

公开问题: 0

类型:扩展

v0.2.4 2018-05-06 03:45 UTC

This package is auto-updated.

Last update: 2024-09-15 12:43:41 UTC


README

Laravel / Lumen Twilio 实现

composer require kevupton/twilavel

1. 安装服务提供者

<?php
// add directly from the app 
$app->register(\Kevupton\Twilavel\Providers\TwilavelServiceProvider::class);

或者

所有服务提供者都已注册在 config/app.php 配置文件中。

<?php

'providers' => [
    // Other Service Providers

    \Kevupton\Twilavel\Providers\TwilavelServiceProvider::class,
],

2. 配置

.env 配置

TWILIO_LOG_OVERRIDE=false
TWILIO_TOKEN=your_token
TWILIO_SID=your_sid
TWILIO_FROM=SwagApp

执行 php artisan vendor:publish 以获取完整的配置文件。

配置:twilio.php

<?php

return array(

    // set to true to prevent the
    'log_override' => env('TWILIO_LOG_OVERRIDE', false),

    // Your Account SID and Auth Token from twilio.com/console
    'sid' => env('TWILIO_SID'),
    'token' => env('TWILIO_TOKEN'),

    // the default from which messages will be sent from
    'from' => env('TWILIO_FROM', 'Twilavel'),
);

3. 使用方法

使用门面

<?php 

\Twilio::message(new \Kevupton\Twilavel\Messages\BasicMessage('+61 123 456 789', 'Custom Body', 'From'));

使用辅助函数

<?php 

send_basic_message('+61 123 456 789', 'Custom Body', 'From');

自定义消息示例

<?php

namespace App\Twilio\Messages;

use \Kevupton\Twilavel\Messages\Message;
use App\Models\User;

class VerifyPhone extends Message {

    const FROM = '+61123456789';

    /** @var string custom verification code */
    private $code;

    public function __construct(User $user, $mobile = null)
    {
        // use mobile other use user mobile
        $number = $mobile?: $user->mobile;
        parent::__construct($number, self::FROM);
        
        // get a random code
        $this->code = $user->generateMobileVerificationCode($number);
    }

    public function getBody()
    {
        return 'Your verification code: ' . $this->code;
    }
}

使用自定义消息

<?php

send_message(new VerifyPhone($user, '+61123456789'));

5. 日志记录

您可以选择在开发环境中覆盖发送 API 请求,并仅在日志中查看它们。只需在您的 .env 中设置 TWILIO_LOG_OVERRIDE=true 即可。

贡献

随时提交拉取请求。任何帮助都受欢迎(Y)