ahmad-klfr / sms
该软件包最新版本(dev-master)没有可用的许可证信息。
这是 Opilo(www.opilo.com)Web 服务的一个客户端
dev-master
2016-01-23 08:04 UTC
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~6.0
Requires (Dev)
- mockery/mockery: *
- phpunit/phpunit: 4.*
This package is not auto-updated.
Last update: 2024-09-20 09:26:38 UTC
README
为了通过 opilo.com 控制面板发送和接收短信,您首先需要创建 OpiloClient\V2\HttpClient 类的实例对象。为此,您需要先在 配置页面 中配置您的 Web 服务。
创建客户端对象
use OpiloClient\Configs\Account; use OpiloClient\Configs\ConnectionConfig; use OpiloClient\V2\HttpClient; ... $config = new ConnectionConfig('http://bpanel.opilo.com'); $account = new Account('YOUR_WEBSERVICE_USERNAME'), 'YOUR_WEBSERVICE_PASSWORD'); $client = new HttpClient($config, $account);
发送短信
发送单个短信
use OpiloClient\Request\OutgoingSMS; ... $message = new OutgoingSMS('3000****', '0912*******', 'Hello World!'); $responses = $client->sendSMS($message);
一次性发送一批短信
$messages = [ new OutgoingSMS('3000****', '0912*******', 'Hello World!'), new OutgoingSMS('3000****', '0912*******', 'Hello World!'), ]; $response = $client->sendSMS($messages);
解析 sendSMS() 的返回值
use OpiloClient\Response\SMSId; use OpiloClient\Response\SendError; ... for ($i = 0; $i < count($response); $i++) { if ($response[$i] instanceof SMSId) { //store $response[$i]->id as the id of $messages[$i] in your database and schedule for checking status if needed } else //$response[$i] instanceof SendError { //It could be that you run out of credit, the line number is invalid, or the receiver number is invalid. //To find out more examine $response[$i]->error and compare it against constants in SendError class } }
分页检查收件箱
$minId = 0; while (true) { $inbox = $client->checkInbox($minId); $messages = $inbox->getMessages(); if (count($messages)) { foreach ($messages as $message) { //Process $message->opiloId(), $message->getFrom(), $message->getTo(), $message->getText(), and $message->getReceivedAt() and store them in your database $minId = max($minId, $message->getOpiloId() + 1); } } else { //no new SMS //Store $minId in your database for later use of this while loop! You don't need to start from 0 tomorrow! break; } }
检查已发送短信的投递状态
$opiloIds = $yourDatabaseRepository->getArrayOfOpiloIdsOfMessagesSentViaSendSMSFunction(); $response = $client->checkStatus($opiloIds); foreach ($response->getStatusArray() as $opiloId => $status) { //process and store the status code $status->getCode() for the SMS with Id $opiloId //Take a look at constants in OpiloClient\Response\Status class and their meanings }
获取您的短信余额
$numberOfSMSYouCanSendBeforeNeedToCharge = $client->getCredit()->getSmsPageCount();
异常处理
如果凭证或配置无效,或者存在网络或服务器错误,HttpClient 中的所有函数可能会抛出 CommunicationException。请准备好适当地捕获异常。
use OpiloClient\Response\CommunicationException; ... try { ... $client->sendSMS(...); ... } catch (CommunicationException $e) { //process the exception by comparing $e->getCode() against constants defined in CommunicationException class. }