opilo/webservice

此包的最新版本(1.0.1)没有可用的许可信息。

这是Opilo(www.opilo.com)Web服务的客户端

1.0.1 2016-11-08 14:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:09:21 UTC


README

用法

首先,通过Composer PHP依赖管理器安装opilo/webservice包。

composer require opilo/webservice

注意:如果您想了解更多关于composer的信息,请访问https://getcomposer.org/

安装composer包后,为了通过opilo.com面板发送和接收短信,您需要创建OpiloClient\V2\HttpClient类的实例对象。为了做到这一点,首先您需要在配置页面中配置您的webservice。

创建客户端对象

use OpiloClient\V2\HttpClient;
...
$client = new HttpClient('YOUR_WEBSERVICE_USERNAME', 'YOUR_WEBSERVICE_PASSWORD');

发送短信

发送单个短信

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);

用户定义的id

如果在网络错误的情况下,您可能需要重新发送您的短信以确保它已送达Opilo服务器,但您不希望它被发送到目标多次。为了防止重复的短信,您可以将唯一的字符串设置为OutgoingSMS对象的uid字段。如果收到具有重复uid的短信,Opilo服务器将丢弃该短信并返回一个带有布尔duplicate标志的SMSId对象。只对过去24小时内发送的消息进行uid的重复检查。

$messages = [
    new OutgoingSMS('3000****', '0912*******', 'Dont send this twice!', $some_unique_identifier_for_this_sms),
];

解析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
    } elseif ($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.
}

Laravel支持

要在Laravel中使用Web服务,请将OpiloClient\Laravel\OpiloServiceProvider注册到您的config/app.php中。

    'providers' => [
        // Add this to end of 'providers' array
        OpiloClient\Laravel\OpiloServiceProvider::class,
    ]

您还可以添加外观以便更方便地使用Web服务。

    'aliases' => [
        // Add this to end of 'aliases' array
        'Opilo' => OpiloClient\Laravel\HttpClient::class,
    ]

要发布opilo配置文件,请运行php artisan vendor:publish --provider="OpiloClient\Laravel\OpiloServiceProvider"。将变量OPILO_WS_USERNAMEOPILO_WS_PASSWORD放入您的.env文件中。