donzo24/orange-sms

一个用于与中东和非洲的orange sms api交互的PHP库。

dev-master 2024-02-27 12:49 UTC

This package is auto-updated.

Last update: 2024-09-27 13:58:56 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

描述

一个用于与中东和非洲的orange sms api交互的PHP库。

安装

使用composer

$ composer require donzo24/orange-sms

用法

首先你需要解决一个SMSClient实例

use Donzo24\Orange\SMS\SMS;
use Donzo24\Orange\SMS\Http\SMSClient;

/**
* if you already have a valid access token
* */
$client = SMSClient::getInstance('<your_access_token>');

// OR

/**
* directly using <client_id> and <client_secret>
* */
$client = SMSClient::getInstance('<client_id>', '<client_secret>');

下一步,创建一个SMS对象,并传入$client

$sms = new SMS($client);

现在你就可以使用了

// prepare and send an sms in a fluent way
$sms->message('Hello, my dear...')
    ->from('+237690000000')
    ->to('+237670000000')
    ->send();

现在你通过$sms对象可以访问完整的orange sms api

// sending SMS.
$response = $sms->to('+237670000000')
                ->from('+237690000000', <'optional_sender_name>')
                ->message('Hello, world!')
                ->send();

// checking your balance(remaining sms units)
// with optional country code filter ie: CIV
$response = $sms->balance('<country_code>');

// checking SMS orders history
// with optional country code filter ie: CMR
$response = $sms->ordersHistory('<country_code>');

// checking SMS statistics
// with optional country code filter
// and optional appID filter
$response = $sms->statistics('<country_code>', '<app_id>');

// setting the SMS DR notification endpoint
// '<your_backend_notification_url>' $url
// '<sender address>' $sender = '+237690000000'
$response = $sms->setDeliveryReceiptNotificationUrl($url, $sender);

// checking the SMS DR notification endpoint
// '<your_last_registered_endpoint_ID>' $id
$response = $sms->checkDeliveryReceiptNotificationUrl($id);

// delete the SMS DR notification endpoint
// '<last_registered_endpoint_ID>' $id
// '<sender address>' $sender = '+237690000000'
$response = $sms->deleteDeliveryReceiptNotificationUrl($id, $sender);

所有json响应将自动转换为array

请务必查阅官方文档,以了解每次调用预期的response

访问令牌

当你使用client_idclient_secret解决SMSClient实例时,将从API服务器获取一个新的访问令牌,并自动设置在实例上,同时包括其有效期的秒数。

我们建议将令牌(可能存储到数据库中)保存以供将来使用,至少在其有效期内。这有助于加快对API的请求。

使用getToken()getTokenExpiresIn()从实例获取这些值

use Donzo24\Orange\SMS\Http\SMSClient;

$client = SMSClient::getInstance('<client_id>', '<client_secret>');

// get the token
$token = $client->getToken();

// get the token lifetime in seconds
$tokenExpiresIn = $client->getTokenExpiresIn();

如果您愿意,您也可以不解决客户端实例,直接使用静态authorize方法获取您的访问令牌

$response = SMSClient::authorize('<client_id>', '<client_secret>');

这将返回一个$response,其形式如下数组

[
 "token_type" => "Bearer",
 "access_token" => "i6m2iIcY0SodWSe...L3ojAXXrH",
 "expires_in" => "7776000"
]

SSL证书检查问题

如果您在本地环境中遇到SSL证书检查问题,您可以在开始与API交互之前,使用以下行暂时禁用检查。

仅用于测试目的。您永远不应该在生产服务器上这样做。

\Donzo24\Orange\SMS\Http\SMSClientRequest::verify(false);

许可协议

Donzo24 orange-sms是一个开源软件,许可协议为MIT许可