teknasyon/laravel-sms

基于 simplesoftwareio/simple-sms 并使用 Verimor 驱动的分支。

v0.16.2 2018-07-31 16:38 UTC

This package is auto-updated.

Last update: 2024-09-12 04:30:01 UTC


README

Build Status Latest Stable Version Latest Unstable Version License Total Downloads

简介

Simple SMS 是一款易于使用的 Laravel 扩展包,可以发送和接收短信/MMS 消息到手机。它目前支持通过无线运营商提供的免费电子邮件网关发送短信消息。该包还支持 6 个付费服务,包括 Call Fire, EZTexting, LabsMobile, Mozeo, Nexmo,Twilio.

需求

Laravel 5

  • PHP: >= 5.5
  • Guzzle >= 6.0

配置

Laravel 4

请阅读 Laravel 4 的 文档。

Composer

首先,将 Simple SMS 包添加到您的 composer/json 文件中的 require 部分

composer require mrabbani/laravel_infobip

接下来,运行 composer update 命令。这将安装包到您的 Laravel 应用中。

服务提供者

一旦您将包添加到 composer 文件中,您需要将服务提供者注册到 Laravel 中。

在您的 config/app.php 配置文件中的 providers 数组中添加 SimpleSoftwareIO\SMS\SMSServiceProvider::class

别名

最后,注册 Facade。

在您的 config/app.php 配置文件中的 aliases 数组中添加 'SMS' => SimpleSoftwareIO\SMS\Facades\SMS::class

API 设置

您必须运行以下命令以将配置文件保存到您的本地应用

php artisan vendor:publish

这将复制配置文件到您的 config 文件夹。

未能运行 vendor:publish 命令将导致每次运行 composer update 命令后,您的配置文件被覆盖。

驱动器配置

调用驱动器

此驱动器通过 Call Fire 服务发送和接收所有消息。它是一个快速且可靠的提供商,包括许多功能,如漏斗活动和语音服务。

config 文件中填写正确的设置以使用此驱动器。您可以在您的 CallFire 账户下找到这些设置,然后选择 API Access。

return [
    'driver' => 'callfire',
    'from' => 'Not Use For Call Fire',
    'callfire' => [
        'app_login' => 'Your App Login',
        'app_password' => 'Your App Password'
    ],
];

注意:所有来自 CallFire 的消息都来自同一个短号(67076)

电子邮件驱动器

电子邮件驱动器通过配置的电子邮件驱动器为 Laravel 发送所有消息。此驱动器使用无线运营商的电子邮件网关将短信消息发送到手机。使用电子邮件驱动器的最大好处是完全免费。

此驱动器的唯一设置是 from 设置。只需输入您想要发送消息的电子邮件地址即可。

return [
    'driver' => 'email',
    'from' => 'example@example.com',
];

如果消息没有发送,请确保您首先能够通过 Laravel 发送电子邮件。

以下是通过电子邮件网关驱动程序当前支持的。

使用此驱动程序,您必须知道移动电话的无线提供商。

小心!并非所有无线运营商都支持全球的电子邮件网关。

一些运营商会在短信消息中添加fromto地址来轻微修改消息。

未测试的网关意味着我们尚未能够确认网关是否与移动运营商兼容。如果您在这些运营商之一,请提供反馈。

EZTexting

此驱动程序通过EZTexting服务发送所有消息。EZTexting具有许多不同的选项,这些选项已被证明是可靠且快速的。

填写config文件以正确的设置启用EZTexting。

return [
    'driver' => 'eztexting',
    'from' => 'Not Use For EZTexting',
    'eztexting' => [
        'username' => 'Your Username',
        'password' => 'Your Password'
    ],
];

要为此服务启用receive(),您必须访问EZTexting设置页面。为要转发到您的Web应用程序的消息启用转发API关键字API

注意:所有来自EZTexting的消息都来自相同的短号码(313131)

LabsMobile 驱动器

此驱动程序通过LabsMobile服务发送所有消息。这些设置可以在您的API设置页面上找到。

return [
    'driver' => 'labsmobile',
    'from' => 'Sender',
    'labsmobile' => [
        'client' => 'Your Client Key',
        'username' => 'Your Username',
        'password' => 'Your Password',
        'test' => '1 for simulate mode; 0 for real sendings'
    ]
];

Mozeo 驱动器

此驱动程序通过Mozeo服务发送所有消息。这些设置可以在您的API设置页面上找到。

return [
    'driver' => 'mozeo',
    'from' => 'Not Used With Mozeo',
    'mozeo' => [
        'companyKey' => 'Your Company Key',
        'username' => 'Your Username',
        'password' => 'Your Password'
    ]
];

注意:所有来自Mozeo的消息都来自相同的短号码(24587)

Nexmo 驱动器

此驱动程序通过Nexmo消息服务发送消息。它非常可靠,能够向全球的手机发送消息。

return [
    'driver' => 'nexmo',
    'from' => 'Company Name',
    'nexmo' => [
        'key'       => 'Your Nexmo API Key',
        'secret'    => 'Your Nexmo API Secret'
    ]
];

要启用receive()消息,您必须设置请求URL。

Twilio 驱动器

此驱动程序通过Twilio消息服务发送消息。它非常可靠,能够向全球的手机发送消息。

return [
    'driver' => 'twilio',
    'from' => '+15555555555', //Your Twilio Number in E.164 Format.
    'twilio' => [
        'account_sid' => 'Your SID',
        'auth_token' => 'Your Token',
        'verify' => true,  //Used to check if messages are really coming from Twilio.
    ]
];

强烈建议启用verify选项。此设置执行额外的安全检查,以确保消息来自Twilio,而不是被欺骗。

要启用receive()消息,您必须设置请求URL。选择您希望启用的号码,然后输入您的请求URL。此请求应为POST请求。

Infobip 驱动器

此驱动程序通过Infobib消息服务发送消息。它非常可靠,能够向全球的手机发送消息。

return [
    'driver' => 'infobip',
    'from' => 'InfoSMS',
    'infobip'=> [
         'username' => 'username of infobip',
         'password' => 'password of infobip'
     ]
];

更多信息请参阅Infobip API开发者中心

##驱动程序支持

由于每个API的差异,并非所有驱动程序都支持每个方法。以下表格概述了每个驱动程序支持的功能。

用法

基本用法

简单的短信服务与Laravel邮件服务提供者类似。如果您熟悉这一点,那么短信应该感觉像家一样。发送短信的最基本方法如下

//Service Providers Example
SMS::send('simple-sms::welcome', $data, function($sms) {
    $sms->to('+15555555555');
});

//Email Driver Example
SMS::send('simple-sms::welcome', $data, function($sms) {
    $sms->to('+15555555555', 'att');
});

第一个参数是要使用的视图文件。第二个是您要传递给视图的数据。最后一个参数是一个回调,将设置message闭包上的所有选项。

发送

send方法通过配置的驱动程序使用Laravel视图文件发送短信。

SMS::send($view, Array $data, function($sms) {
    $sms->to('+15555555555');
}
SMS::send('simple-sms::welcome', $data, function($sms) {
    $sms->to('+15555555555');
});

可以通过传递一个字符串而不是视图来发送一条简单的消息,而不需要创建视图。

SMS::send($message, [], function($sms) {
    $sms->to('+15555555555');
}
SMS::send('This is my message', [], function($sms) {
    $sms->to('+15555555555');
});

驱动程序

driver方法将在运行时切换提供程序。

//Will send through default provider set in the config file.
SMS::queue('simple-sms::welcome', $data, function($sms) {
    $sms->to('+15555555555');
});

SMS::driver('twilio');

//Will send through Twilio
SMS::queue('simple-sms::welcome', $data, function($sms) {
    $sms->to('+15555555555');
});

队列

queue方法将消息排队以稍后发送,而不是立即发送消息。这允许通过卸载不常见的处理时间来加快消费者的响应时间。类似于Laravel的邮件系统,队列也有queueOnlaterlaterOn方法。

SMS::queue('simple-sms::welcome', $data, function($sms) {
    $sms->to('+15555555555');
});

如果未在Laravel中配置队列服务,则queue方法将回退到send方法。

假装

pretend方法将简单地创建一个日志文件,声明已“发送”短信消息。这有助于测试您的配置设置是否正确,而无需发送实际的消息。

SMS::pretend('simple-sms::welcome', $data, function($sms) {
    $sms->to('+15555555555');
});

您还可以将pretend配置选项设置为true,以便所有短信消息都假装已发送。

`/app/config/simplesoftwareio/simple-sms/config.php`
return array(
    'pretend' => true,
);

接收

Simple SMS支持推送短信消息。您必须首先通过上面的配置设置与您的服务提供商进行配置。

Route::post('sms/receive', function()
{
    SMS::receive();
}

接收方法将返回一个IncomingMessage实例。您可以从该实例请求任何数据,例如

Route::post('sms/receive', function()
{
    $incoming = SMS::receive();
    //Get the sender's number.
    $incoming->from();
    //Get the message sent.
    $incoming->message();
    //Get the to unique ID of the message
    $incoming->id();
    //Get the phone number the message was sent to
    $incoming->to();
    //Get the raw message
    $incoming->raw();
}

raw方法返回驱动程序支持的所有数据。这可能有助于获取只有某些服务提供商提供的有关信息。

Route::post('sms/receive', function()
{
    $incoming = SMS::receive();
    //Twilio message status
    echo $incoming->raw()['status'];
}

这将返回Twilio驱动程序上消息的状态。

来自raw方法的数据在其他服务提供商上可能不起作用。每个提供商在每次请求中发送不同的值。

检查消息

此方法将从服务提供商检索消息数组。数组中的每个消息都是一个IncomingMessage对象。

$messages = SMS::checkMessages();
foreach ($messages as $message)
{
    //Will display the message of each retrieve message.
    echo $message->message();
}

checkMessages方法支持一个options变量,可以将一些设置传递给每个服务提供商。查看每个服务提供商的API以查看可以传递哪些options

有关每个服务提供商的更多信息,可以在它们的API文档中找到。

获取消息

您可以通过简单的调用来通过其ID检索消息。这将返回一个IncomingMessage对象。

$message = SMS::getMessage('aMessageId');
//Prints who the message came from.
echo $message->from();

外发消息封装

为什么使用附件?

我们使用附件来允许像队列方法这样的功能。能够轻松保存消息附件提供了更大的灵活性。

to方法添加一个电话号码,消息将发送到该号码。

//Service Providers Example
SMS::send('simple-sms::welcome', $data, function($sms) {
    $sms->to('+15555555555');
    $sms->to('+14444444444');
});
//Email Driver
SMS::send('simple-sms::welcome', $data, function($sms) {
    $sms->to('15555555555', 'att);
    $sms->to('14444444444', 'verizonwireless);
});

对于电子邮件驱动程序,需要运营商才能使用正确的电子邮件网关。请参阅上面的表格以获取受支持的运营商列表。

来自

from方法将设置发送消息的地址。

SMS::send('simple-sms::welcome', $data, function($sms) {
    $sms->from('+15555555555');
});

attachImage

attachImage方法将向消息添加图像。这将也将消息转换为MMS,因为SMS不支持图像附件。

//Email Driver
SMS::send('simple-sms::welcome', $data, function($sms) {
    $sms->attachImage('/local/path/to/image.jpg');
});
//Twilio Driver
SMS::send('simple-sms::welcome', $data, function($sms) {
    $sms->attachImage('/url/to/image.jpg');
});

目前仅支持电子邮件和Twilio驱动程序。

入站消息

所有传入的消息都会生成一个IncomingMessage对象。这使得在多个服务提供商之间以统一的方式检索信息变得容易。

原始

raw方法返回服务提供商提供的原始数据。

$incoming = SMS::getMessage('messageId');
echo $incoming->raw()['status'];

每个服务提供商在其请求中提供不同的信息。请参阅它们的API文档以获取有关可以从raw请求中获取什么的信息。

来自

此方法返回消息的来源电话号码。

$incoming = SMS::getMessage('messageId');
echo $incoming->from();

to方法返回消息发送到的电话号码。

$incoming = SMS::getMessage('messageId');
echo $incoming->to();

Id

此方法返回消息的唯一ID。

$incoming = SMS::getMessage('messageId');
echo $incoming->id();

消息

最后但同样重要的是,此方法返回SMS的实际消息。

$incoming = SMS::getMessage('messageId');
echo $incoming->message();