cammac / sms
此包是为Laravel设计的,用于发送/接收(轮询/推送)短信。目前支持smsgw.net、mobily.ws、CalLFire、EZTexting、电子邮件网关、FlowRoute、LabsMobile、Mozeo、Nexmo、Plivo、Twilio和Zenvia
Requires
- illuminate/support: ^5.5
- jeremeamia/superclosure: ^2.4
- simplesoftwareio/simple-sms: ^3.1
Requires (Dev)
- codedungeon/phpunit-result-printer: ^0.4.4
- mockery/mockery: ^1.0
- orchestra/testbench: ~3.5
- phpunit/phpunit: ^6.5
This package is auto-updated.
Last update: 2024-09-09 01:35:10 UTC
README
简介
Simple SMS 是一个易于使用的Laravel包,可以将发送和接收短信/MMS消息到手机的能力添加到您的Web应用程序中。它目前支持通过无线运营商提供的电子邮件网关免费发送短信消息。该包还支持9个付费服务,包括Call Fire、EZTexting、FlowRoute、LabsMobile、Mozeo、Nexmo、Plivo、Twilio、Zenvia、SMSgw和Mobily.ws。
要求
Laravel 5
- PHP: >= 7.x
- Guzzle >= 6.0
配置
Composer
$ composer require cammac/sms
服务提供者
如果你的Laravel版本低于5.5,你需要在config/app.php
的providers
列表中添加PostMarkServiceProvider
在config/app.php
中
\Cammac\LaravelSms\LaravelSmsServiceProvider::class,
别名
如果你的Laravel版本低于5.5,你需要在config/app.php
的providers
列表中添加PostMarkServiceProvider
在config/app.php
中
'SMS' => SimpleSoftwareIO\SMS\Facades\SMS::class,
在你的config/app.php
配置文件的aliases
数组中添加
API设置
你必须运行以下命令将你的配置文件保存到你的本地应用
$ php artisan vendor:publish
这将把配置文件复制到你的config
文件夹。
驱动程序配置
SMSgw 驱动程序
此驱动程序通过smsgw.net服务发送短信。它是一个非常快速和可靠的服务提供商。
在config
文件中填写正确的设置以使用此驱动程序。您可以在您的SMSgw账户下找到这些设置
return [
'driver' => 'smsgw',
'from' => 'Company A',
'smsgw' => [
'username' => 'Your username',
'password' => 'Your Password'
],
];
Mobily.ws 驱动程序
此驱动程序通过Mobily.ws服务发送和接收所有短信。
在config
文件中填写正确的设置以使用此驱动程序。
return [
'driver' => 'mobilyws',
'from' => 'Company A',
'mobilyws' => [
'username' => 'Your username',
'password' => 'Your Password'
],
];
Call Fire 驱动程序
此驱动程序通过Call Fire服务发送和接收所有短信。它是一个非常快速和可靠的服务提供商,包括许多功能,如滴灌活动和语音服务。
在config
文件中填写正确的设置以使用此驱动程序。您可以在您的CallFire账户下找到这些设置,然后选择API访问。
return [
'driver' => 'callfire',
'from' => 'Not Use For Call Fire',
'callfire' => [
'app_login' => 'Your App Login',
'app_password' => 'Your App Password'
],
];
电子邮件驱动程序
电子邮件驱动程序通过Laravel配置的电子邮件驱动程序发送所有消息。此驱动程序使用无线运营商的电子邮件网关将短信消息发送到手机。使用电子邮件驱动程序的最大好处是完全免费。
此驱动程序的唯一设置是from
设置。只需输入您希望发送消息的电子邮件地址。
return [
'driver' => 'email',
'from' => 'example@example.com',
];
如果消息没有发送,请确保您首先能够通过Laravel发送电子邮件。
目前支持使用电子邮件网关驱动程序。
您必须知道手机的无线运营商才能使用此驱动程序。
小心!并非所有无线运营商在世界各地都支持电子邮件网关。
某些运营商会在短信消息中添加
from
和to
地址,对消息进行轻微修改。
EZTexting
此驱动程序通过EZTexting服务发送所有消息。EZTexting具有许多不同的选项,这些选项已被证明是可靠且快速的。
在config
文件中填写正确的设置以启用EZTexting。
return [
'driver' => 'eztexting',
'from' => 'Not Use For EZTexting',
'eztexting' => [
'username' => 'Your Username',
'password' => 'Your Password'
],
];
要为此服务启用receive()
,您必须访问EZTexting设置页面。为要转发到您的Web应用程序的消息启用Forwarding API
和Keyword API
。
注意:所有来自EZTexting的消息都来自同一短号(313131)
FlowRoute
此驱动程序通过FlowRoute服务发送所有消息。
在config
文件中填写正确的设置以启用EZTexting。
return [
'driver' => 'flowroute',
'from' => 'Not Use For EZTexting',
'flowroute' => [
'access_key' => 'Your Access Key',
'secret_key' => 'Your Secret Key'
],
];
要为此服务启用receive()
,您必须访问FlowRoute设置页面。为要转发到您的Web应用程序的消息启用Forwarding API
和Keyword API
。
LabsMobile 驱动程序
此驱动程序通过LabsMobile服务发送所有消息。这些设置可以在您的API设置页面找到。
return [
'driver' => 'labsmobile',
'from' => 'Sender',
'labsmobile' => [
'username' => 'Your Username',
'password' => 'Your Password',
]
];
日志驱动程序
日志驱动程序只是一个简单的测试驱动程序。这将记录在您的storage/logs
文件夹中发送了短信消息。
return [
'driver' => 'log',
];
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' => [
'api_key' => 'Your Nexmo API Key',
'api_secret' => 'Your Nexmo API Secret',
'encoding' => 'unicode', // Can be `unicode` or `gsm`
]
];
要启用receive()
消息,您必须设置请求URL。
Plivo 驱动程序
此驱动程序通过Plivo消息服务发送消息。
return [
'driver' => 'plivo',
'from' => 'Company Name',
'plivo' => [
'auth_id' => 'Your Auth ID.',
'auth_token' => 'Your Auth Token.'
]
];
要启用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.
]
];
Zenvia 驱动程序
本驱动通过[Zenvia](http://www.zenvia.com.br/)消息服务发送消息,该服务在巴西非常流行。
return [
'driver' => 'zenvia',
'from' => 'Your Number',
'zenvia' => [
'account_key' => 'Your Account Key.',
'passcode' => 'Your Passcode.'
'call_back_option' => 'The URL you wish to have receive messages sent to.'
]
];
JustSend 驱动程序
本驱动通过JustSend消息服务发送消息,该服务仅在波兰可用。
return [
'driver' => 'justsend',
'from' => 'From text',
'justsend' => [
'api_key' => 'Your API Key.'
]
];
from
可以是以下之一
ECO
- 最低成本类型(每条短信约0.03 PLN)。短信将发送到随机的短号码。- 以下之一:
INFO
、INFORMACJA
、KONKURS
、NOWOSC
、OFERTA
、OKAZJA
、PROMOCJA
、SMS
- 每条短信约0.06 PLN。 - 任何其他文本(不能仅是数字) - 每条短信约0.07 PLN。
驱动程序支持
由于每个API的差异,并非所有驱动程序都支持每种方法。以下表格概述了每个驱动程序支持的功能。
| 驱动程序 | 发送 | 排队 | 检查消息 | 获取消息 | 接收 | | --- | --- | --- | --- | --- | --- | --- | | Call Fire | 是 | 是 | 是 | 是 | 否 | | E-Mail | 是 | 是 | 否 | 否 | 否 | | EZTexting | 是 | 是 | 是 | 是 | 是 | | FlowRoute | 是 | 是 | 是 | 是 | 是 | | LabsMobile | 是 | 是 | 否 | 否 | 否 | | Log | 是 | 是 | 否 | 否 | 否 | | Mozeo | 是 | 是 | 否 | 否 | 否 | | Nexmo | 是 | 是 | 是 | 是 | 是 | | Plivo | 是 | 是 | 是 | 是 | 是 | | Twilio | 是 | 是 | 是 | 是 | 是 | | Zenvia | 是 | 是 | 是 | 是 | 是 | | JustSend | 是 | 是 | 否 | 否 | 否 |
使用方法
基本用法
简单的短信服务与Laravel邮件服务提供商的工作方式非常相似。如果您熟悉这个,那么短信应该感觉像家一样。发送短信的最基本方法是使用以下方法
//Service Providers Example
SMS::send('Your SMS Message', null, function($sms) {
$sms->to('+15555555555');
});
//Service Providers Using A Laravel View
//$data is the information that will be passed onto the view file.
SMS::send('laravel::viewFile', $data, function($sms) {
$sms->to('+15555555555');
});
//Email Driver Example
SMS::send('Your SMS Message', null, function($sms) {
$sms->to('+15555555555', 'att');
});
第一个参数是要使用的视图文件。第二个是要传递给视图的数据。最后一个参数是一个回调,它将为message
闭包设置所有选项。
发送
send
方法通过配置的驱动程序使用Laravel视图文件发送短信。
SMS::send($view, Array $data, function($sms) {
$sms->to('+15555555555');
}
SMS::send('laravel::view', $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('Your SMS Message', $data, function($sms) {
$sms->to('+15555555555');
});
SMS::driver('twilio');
//Will send through Twilio
SMS::queue('Your SMS Message', $data, function($sms) {
$sms->to('+15555555555');
});
排队
queue
方法将消息排队以稍后发送而不是立即发送。这允许消费者通过卸载不常见的处理时间来获得更快的响应时间。像Laravel的邮件
系统一样,队列也具有queueOn
、later
和laterOn
方法。
SMS::queue('Your SMS Message', $data, function($sms) {
$sms->to('+15555555555');
});
如果未在
Laravel
中配置队列服务,则queue
方法将回退到send
方法。
接收
简单的短信支持推送短信消息。您必须首先按照上述配置设置与您的服务提供商进行配置。
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 Provider Example
SMS::send('Your SMS Message', $data, function($sms) {
$sms->to('+15555555555');
$sms->to('+14444444444');
});
对于电子邮件驱动程序,需要提供运营商信息,以便使用正确的电子邮件网关。请参阅上面的表格,了解接受运营商的列表。
发送方
from
方法将设置发送消息的地址。
SMS::send('Your SMS Message', $data, function($sms) {
$sms->from('+15555555555');
});
attachImage
attachImage
方法将向消息添加一张图片。这也将消息转换为MMS,因为SMS不支持图片附件。
//Service Provider Driver
SMS::send('Your SMS Message', $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();