Victorybiz/Unified-SMS

适用于Laravel和PHP(非Laravel)的统一短信库包,可通过多个可替换的驱动发送短信。

v1.1.7 2020-01-05 23:29 UTC

This package is auto-updated.

Last update: 2024-09-06 09:58:20 UTC


README

GitHub release Software License Build Status Packagist

适用于Laravel和PHP(非Laravel)的统一短信库包,可通过多个可替换的驱动发送短信。

支持的驱动和服务

当前包含以下驱动

  • null - Null(短信,仅限语音短信)- 用于测试目的,此驱动不执行任何操作。
  • routemobile - RouteMobile - RouteSMS(仅限短信)
  • nexmo - Nexmo(仅限短信)
  • moreify - Moreify(仅限短信)
  • betasms - BetaSMS(仅限短信)
  • multitexter - MultiTexter(仅限短信)

安装

使用composer安装,从命令行运行

$ composer require victorybiz/unified-sms

Laravel项目

或者,您可以将 "victorybiz/unified-sms": "^1.1" 添加到 composer.json 文件的 require 部分,然后您需要运行 composer installcomposer update 来下载它并更新自动加载器。

如果您使用 Laravel >= 5.5,则可以跳过此步骤并转到 配置

如果您使用 Laravel < 5.5,您需要将服务提供程序注册到应用程序中。打开 config/app.php 并定位到 providers 键。

'providers' => [

    Victorybiz\UnifiedSMS\UnifiedSMSServiceProvider::class,

]

并在 config/app.php 中添加 UnifiedSMS 别名

'aliases' => [

	'UnifiedSMS' => Victorybiz\UnifiedSMS\Facades\UnifiedSMSFacade::class,

]

配置(Laravel)

您必须发布包的配置文件(unified-sms.php 也随此发布),并将 UnifiedSMS 别名添加到 config/app.php

php artisan vendor:publish --tag=unified-sms

打开 config/unified-sms.php,使用 env 变量设置您的默认驱动和API凭据。

Laravel项目中的使用

请使用 UnifiedSMS 门面

use UnifiedSMS;

您已准备好发送短信

$msg = [
			'from' => 'Your sender ID here', // default_sender_id set in config file will be used if this line is removed or comment out
			'to' => 'The recipent mobile number, international format without the leading plus (+)',
			'text' => 'Your text message here.',
	   ];
$response = UnifiedSMS::sendSMS($msg);

PHP(非Laravel)项目中的使用

在您的PHP脚本中包含供应商的自动加载文件。

require_once 'path/to/vendor/autoload.php';

在任何项目目录中创建一个 unified_sms_config.php 文件。将以下块输入到您的 unified_sms_config.php 并设置您的默认驱动和API凭据。

<?php
$unified_sms_config = [
    /*
    |--------------------------------------------------------------------------
    | Default SMS driver
    |--------------------------------------------------------------------------
    | This option controls the default SMS driver to use.
    |
    | Supported: "null", "routemobile", "nexmo", "moreify", "betasms", "multitexter"
    */
    'default_sms_driver' => 'null',
	
	/*
    |--------------------------------------------------------------------------
    | Default VOICE SMS driver
    |--------------------------------------------------------------------------
    | This option controls the default Voice SMS driver to use.
    |
    | Supported: "null"
    */
    'default_voice_sms_driver' => 'null',
	
    /*
    |--------------------------------------------------------------------------
    | Drivers
    |--------------------------------------------------------------------------
    | Here you can define the settings for each driver. 
    */
    'drivers' => [
		'null' => [
			'sms' => [
				'default_sender_id' => 'Null',
				'api_key' => 'null'
			],
			'voice' => [
				'default_caller_id' => '+1000',
				'api_key' => 'null',
			],            
		],
		'routemobile' => [
			'sms' => [
				'default_sender_id' => 'INFO',
				'server' => '',
				'port' => '8080',
				'username' => '',
				'password' => '',
			],          
		],
		'nexmo' => [
			'sms' => [
				'default_sender_id' => 'INFO',
				'api_key' => '',
				'api_secret' => '',
				'callback_url' => null, // The webhook endpoint the delivery receipt for this sms is sent to. 
									// If set, it overrides the webhook endpoint you set in Dashboard 
			], 
		],
		'moreify' => [
			'sms' => [
				'project' => '',
				'password' => '',
			], 
		],
		'betasms' => [
			'sms' => [
				'default_sender_id' => 'INFO',
				'username' => '',
				'password' => '',
			], 
		],
		'multitexter' => [
			'sms' => [
				'default_sender_id' => 'INFO',
				'email' => '',
				'password' => '',
				'force_dnd' => true,
			], 
        ],
	],
];

在您的PHP脚本中包含配置文件。

require_once 'path/to/unified_sms_config.php';
use Victorybiz\UnifiedSMS\UnifiedSMS;

$unifiedSMS = new UnifiedSMS($unified_sms_config); 

或者

$unifiedSMS = new \Victorybiz\UnifiedSMS\UnifiedSMS($unified_sms_config);

您已准备好发送短信

$msg = [
			'from' => 'Your sender ID here', // default_sender_id set in config file will be used if this line is removed or comment out
			'to' => 'The recipent mobile number, international format without the leading plus (+)',
			'text' => 'Your text message here.',
	   ];
$response = $unifiedSMS->sendSMS($msg);

驱动器响应

在成功时,$response 将返回JSON数据

{
	"status":true,
	"statusCode":200,
	"statusDescription":"Success",
	"data": {
		"to":"Recipient phone number",
		"messageId":"The Message ID from the driver service provider",
		"_comment":"May include additional data but depends on the response from the driver service provider"
	},
	"driver":"the default driver used"
}

在失败时,$response 将返回JSON数据

{
	"status":false,
	"statusCode":"status error code here",
	"statusDescription":"Status description / message",
	"data":null,
	"driver":"the default driver used"
}

失败状态码为

200				Success
1001			Invalid URL or Missing Params
1002			Invalid credentials
1003			Invalid recipient
1004			Invalid sender
1005			Invalid message
1006			Invalid message type
1007			Invalid delivery
1008			Insufficient credit
1009			Response timeout
1010			Internal error
1011			Account Suspended

错误报告和问题跟踪

请使用问题跟踪器进行错误报告、功能请求、附加网络服务请求和安全问题。

许可证

MIT