moussouba / orange-sms
一个用于与中东和非洲Orange SMS API交互的PHP库。
dev-master
2021-10-30 16:26 UTC
Requires
- php: >= 5.6
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~5.7
This package is auto-updated.
Last update: 2024-09-29 06:08:42 UTC
README
描述
一个用于与中东和非洲Orange SMS API交互的PHP库。
安装
使用composer
$ composer require moussouba/orange-sms
用法
首先您需要解析一个SMSClient
实例
use Mediumart\Orange\SMS\SMS; use Mediumart\Orange\SMS\Http\SMSClient; /** * if you already have a valid access token * */ $client = SMSClient::getInstance('<your_authorization_header>'); next step, create an `SMS` object passing it the `$client` : ```php $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_id
和client_secret
解析SMSClient
实例时,将从API服务器获取一个新的访问令牌,并自动设置在实例上,同时包括其有效期的秒数。
我们建议保存令牌(例如保存到您的数据库中),以便将来使用,至少在其有效期范围内。这有助于加快对API的请求。
使用getToken()
和getTokenExpiresIn()
从实例获取这些值
use Mediumart\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>');
这将返回一个数组形式的响应
[ "token_type" => "Bearer", "access_token" => "i6m2iIcY0SodWSe...L3ojAXXrH", "expires_in" => "7776000" ]
SSL证书检查问题
如果您在本地环境中遇到SSL证书检查问题,您可以使用以下行暂时禁用检查,然后再开始与API交互。
仅用于测试目的。您永远不应该在生产服务器上这样做。
\Mediumart\Orange\SMS\Http\SMSClientRequest::verify(false);
许可协议
Mediumart orange-sms是一个开源软件,采用MIT许可协议。