gajus/nexmore

Nexmore 是 Nexmo RESTful API 的封装。

0.1.0 2014-04-16 20:56 UTC

This package is not auto-updated.

Last update: 2024-09-10 01:14:25 UTC


README

Build Status Coverage Status

Nexmo 通过 REST 和 SMPP 提供高容量 SMS 和 Voice API。Nexmore 是 Nexmo RESTful API 封装。

文档

设置 Nexmo API 凭据

/**
 * @param string $key Nexmo account API key.
 * @param string $secret Nexmo account secret key for signing the API requests.
 * @param string $api_url
 */
$messenger = new \Gajus\Nexmore\Messenger($key, $secret);

发送 SMS 消息

/**
 * A long SMS is split into chunks of 153 chars (in Unicode content messages 67).
 *
 * @see https://help.nexmo.com/entries/24578133-How-multipart-SMS-is-constructed-
 * @see https://docs.nexmo.com/index.php/sms-api/send-message
 * @param string $from
 * @param string $to
 * @param string $text
 * @param array $parameters
 */
$messenger->sms(string $from, string $to, string $text[, array $parameters]);

sms 方法如果参数未知、缺失或不符合格式要求,将抛出 InvalidArgumentException 异常。

如果至少有一条消息未投递,将抛出 \gajus\nexmore\Error_Exception 异常。

try {
    $messenger->sms('Gajus', '447776413499', 'test');
} catch (\Gajus\Nexmore\Exception\InvalidArgumentException $e) {
    // [..]
} catch (\Gajus\Nexmore\Exception\ErrorException $e) {
    // [..]
} catch (\Gajus\Nexmore\Exception\NexmoreException $e) {
    // [..]
} catch (\Exception $e) {
    // [..]
}

TTS (文本到语音)

sms 方法相同,但 TTS 服务不接受发送者 ID(来自参数)。

/**
 * @see https://docs.nexmo.com/index.php/voice-api/text-to-speech
 * @param string $to
 * @param string $text
 * @param array $parameters
 */
$messenger->tts(string $to, string $text[, array $parameters]);

入站事件

Nexmo 提供两种类型的回调

阅读文档的相关部分,了解如何设置回调 URL。

要捕获这些事件之一,请使用 \gajus\nexmore\Listener 类。

/**
 * Listens to Nexmo Delivery Receipt. All knonw receipt $_GET parameters are mapped to
 * a $receipt property. Parameter names are canonicalized.
 *
 * @param string $key
 * @param string $secret
 * @param boolean $debug Debug allows indbound traffic to come from outside of the safe subnet.
 */
$listener = new \Gajus\Nexmore\Listener();

$delivery_receipt = $listener->getDeliveryReceipt();
$inbound_message = $listener->getInboundMessage();

if ($delivery_receipt) {
    var_dump($delivery_receipt);
}

if ($inbound_message) {
    var_dump($inbound_message);
}

请注意,Nexmore 标准化参数名称,并将所有时间输入转换为 UNIX 时间戳。要了解重新映射实现,请参考 Listener 类源代码。

投递回执

array(10) {
    ["sender_id"]=>
    string(11) "12150000025"
    ["recipient_number"]=>
    string(11) "66837000111"
    ["network_code"]=>
    string(5) "52099"
    ["message_id"]=>
    string(16) "000000FFFB0356D2"
    ["status"]=>
    string(9) "delivered"
    ["error_code"]=>
    string(1) "0"
    ["price"]=>
    string(10) "0.02000000"
    ["receipt_timestamp"]=>
    int(1344779940) <== 2012-08-12 13:59:00
    ["message_timestamp"]=>
    int(1344779977) <== 2012-08-12 13:59:37
    ["reference"]=>
    NULL
}

入站消息

array(7) {
    ["type"]=>
    string(4) "text"
    ["recipient_number"]=>
    string(11) "12108054321"
    ["sender_id"]=>
    string(11) "19150000001"
    ["network_code"]=>
    NULL
    ["message_id"]=>
    string(16) "000000FFFB0356D1"
    ["message_timestamp"]=>
    int(1345408703)
    ["text"]=>
    string(26) "This is an inbound message"
}

待办事项

  • 允许直接调用 API 或为帐户功能创建接口。

替代方案

如果您不喜欢 Nexmore 实现,请 提出问题

以下是一些已知的替代方案

如果您知道更多替代方案,请告诉我,我将它们包括在上面的列表中。