mediumart / orange-sms
一个用于与中东和非洲的Orange SMS API交互的PHP库。
2.0.0
2021-12-23 11:31 UTC
Requires
- php: >= 5.6
- guzzlehttp/guzzle: ~6.0|^7.0.1
Requires (Dev)
- mockery/mockery: 0.9.*|1.4.4
- phpunit/phpunit: ~5.7|^9.3.3
README
描述
一个用于与中东和非洲的Orange SMS API交互的PHP库。
安装
使用composer
$ composer require mediumart/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_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
响应都将自动转换为 数组
。
请务必查看 官方文档,以了解每个调用预期的 响应
。
访问令牌
当您使用您的 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>');
这将返回一个数组,如以下形式的 $response
[ "token_type" => "Bearer", "access_token" => "i6m2iIcY0SodWSe...L3ojAXXrH", "expires_in" => "7776000" ]
SSL证书检查问题
如果您在本地环境中遇到SSL证书检查问题,您可以在开始与API交互之前,使用以下行暂时禁用检查:
仅用于测试目的。您永远不应该在生产服务器上这样做。
\Mediumart\Orange\SMS\Http\SMSClientRequest::verify(false);
许可证
Mediumart orange-sms 是一个开源软件,根据 MIT 许可证 许可。