multiotp/smsgateway

基于平面文件的短信网关PHP类,使用开源的Android应用程序

支持包维护!
multiOTP

v1.0.9 2023-09-20 08:18 UTC

This package is auto-updated.

Last update: 2024-09-20 10:37:40 UTC


README

功能

  • 使用开源Android应用程序短信网关的短信网关 - 使用通用版本

许可证

本软件在LGPL-3.0-only许可证下分发。请阅读LICENSE以获取有关软件可用性和分发的信息。

安装与加载

SMSGateway可在Packagist上找到(使用语义版本控制),并通过Composer安装是推荐的方式。只需将以下行添加到您的composer.json文件中

"multiotp/smsgateway": "^1.0"

或者运行

composer require multiotp/smsgateway

请注意,由Composer生成的vendor文件夹和vendor/autoload.php脚本不是SMSGateway的一部分。

或者,如果您不使用Composer,您可以通过下载SMSGateway的zip文件,然后将SMSGateway文件夹的内容复制到PHP配置中指定的一个include_path目录中,然后手动加载每个类文件

<?php
use multiOTP\SMSGateway\SMSGateway;

require 'path/to/SMSGateway/src/SMSGateway.php';

两个简单的示例

<?php
//Import SMSGateway classes into the global namespace
//These must be at the top of your script, not inside a function
use multiOTP\SMSGateway\SMSGateway;

//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance
$smsgateway = new SMSGateway();

// Set the data folder
$smsgateway->setDataPath(__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR);

// Set the shared secret
$smsgateway->setSharedSecret("secret");

$device_id   = "demo";
$to = "+1234567890";
$message = "Demo message";
$device_h = $smsgateway->calculateAuthenticationHash($device_id);

$message_id = $smsgateway->sendMessage($device_id, $to, $message);

echo "Full URL for Android app URL: https://......./?id=$device_id&h=$device_h";
<?php
//Import SMSGateway classes into the global namespace
//These must be at the top of your script, not inside a function
use multiOTP\SMSGateway\SMSGateway;

//Load Composer's autoloader
require 'vendor/autoload.php';

// Please note that implementing a new_message_handling callback function
//  will flag the new messages as read when returning from the function.
function new_message_handling($array) {
  // Handling $array of new received messages
  // [["device_id"    => "device id",
  //   "message_id"   => "message id",
  //   "from"         => "from phone number",
  //   "sms_sent"     => "sms_sent timestamp (ms)",
  //   "sms_received" => "sms_received timestamp (ms)",
  //   "content"      => "message content",
  //   "last_update"  => "last update timestamp (ms)",
  //   "status"       => "UNREAD|READ"
  //  ],
  //  [...]
  // ]
}

function update_handling($array) {
  // Handling $array of status updates for sent messages
  // [["device_id"    => "device id",
  //   "message_id"   => "message id",
  //   "to"           => "to phone number",
  //   "content"      => "content",
  //   "last_update"  => "last update timestamp (ms)",
  //   "status"       => "NEW|PUSHED|PENDING|SENT|DELIVERED|FAILED"
  //  ],
  //  [...]
  // ]
}

function timeout_handling($array) {
  // Handling $array of devices not seen during the last "DeviceTimeout" seconds
  // [["device_id"   => "device_id",
  //   "last_update" => "device_last_update"
  //  ],
  //  [...]
  // ]
}

//Create an instance
$smsgateway = new SMSGateway();

// Set the data folder
$smsgateway->setDataPath(__DIR__ . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR);

// Launch the API server with callback functions definition
$smsgateway->apiServer("secret", "new_message_handling", "update_handling", "timeout_handling");

SMS消息状态

收到的消息

  • 未读:消息从未在网关上读取过
  • 已读:消息已经在网关上读取过

发送的消息

  • 新:新创建的消息,尚未传输到Android设备
  • 推送:消息已推送到Android设备
  • 待定:消息在Android设备中待定
  • 已发送:消息已发送到网关的网络
  • 已送达:消息已由接收者电话接收
  • 失败:消息投递失败,不会重试
  • 丢失:缺少此消息ID的消息,没有状态信息

在线演示

完整的网关实现在这里: 在线SMSGateway演示。点击链接,一切一目了然。您只需安装并配置配套的开源Android应用程序,即可通过此演示网关发送和接收短信(如使用在线演示网关发送第一条短信后所解释的)。
发送消息时,以下信息将返回在http头和html元标签中

  • X-SMSGateway-State:发送消息的状态(NEW|FAILED)
  • X-SMSGateway-State-Url:检查消息状态的完整URL
  • X-SMSGateway-Message-Id:消息ID

文档

如何使用SMSGateway进行常见场景的示例可以在示例文件夹中找到。如果您在寻找一个好的起点,我们建议您从网关示例开始。

Android特定参数

使用adb shell,您应该更改以下两个参数

  • sms_outgoing_check_max_count
  • sms_outgoing_check_interval_ms

每分钟200条消息的限制示例

adb shell
settings put global sms_outgoing_check_max_count 200
settings put global sms_outgoing_check_interval_ms 60000

在这些更改后,您需要重新启动您的安卓手机。

变更日志

请参阅变更日志

就是这样。现在您应该准备好使用SMSGateway了!