定制 opilo/webservice 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

opilo/webservice

Composer 安装命令:

composer require opilo/webservice

包简介

This is a client of Opilo (www.opilo.com) Web Service

README 文档

README

Usage

First, install the opilo/webservice package through Composer PHP dependency manager:

composer require opilo/webservice

Note: if you like to know more about composer, please visit https://getcomposer.org/.

After installing the composer package, in order to send and receive SMS via opilo.com panel, you should create an instance object of class OpiloClient\V2\HttpClient. For that, first you need to configure your webservice in the configuration page.

Create a Client Object

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

Sending SMS

Sending a Single SMS

use OpiloClient\Request\OutgoingSMS;
...
$message = new OutgoingSMS('3000****', '0912*******', 'Hello World!');
$responses = $client->sendSMS($message);

Sending a Batch of SMS at Once

$messages = [
    new OutgoingSMS('3000****', '0912*******', 'Hello World!'),
    new OutgoingSMS('3000****', '0912*******', 'Hello World!'),
];
$response = $client->sendSMS($messages);

User defined ids

In case of network errors, you may resend your SMS to be sure it is delivered to the Opilo server, but you don't want it to be sent to the target more than once. To prevent duplicate SMSes you can set unique strings as uid fields of the OutgoingSMS objects. In case of receiving a SMS with a duplicate uid, the Opilo server drops that SMS and return an SMSId object with a boolean duplicate flag. The duplication of a uid is checked only against the messages sent during the last 24 hours.

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

Parsing The Return Value of 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
    }
}

Check the Inbox by Pagination

$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;
    }
}

Checking the Delivery Status of Sent Messages

$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
}

Getting Your SMS Credit

$numberOfSMSYouCanSendBeforeNeedToCharge = $client->getCredit()->getSmsPageCount();

Exception Handling

All the functions in HttpClient may throw CommunicationException if the credentials or configurations are invalid, or if there is a network or server error. Prepare to catch the exceptions appropriately.

use OpiloClient\Response\CommunicationException;
...
try {
    ...
    $client->sendSMS(...);
    ...
} catch (CommunicationException $e) {
    //process the exception by comparing $e->getCode() against constants defined in CommunicationException class.
}

Laravel support

To use web service in Laravel, register OpiloClient\Laravel\OpiloServiceProvider in your config/app.php.

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

You can also add the facade to use web service more conveniently.

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

To publish opilo config file, run php artisan vendor:publish --provider="OpiloClient\Laravel\OpiloServiceProvider". Put variables OPILO_WS_USERNAME and OPILO_WS_PASSWORD into your .env file.

opilo/webservice 适用场景与选型建议

opilo/webservice 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 415 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 12 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「web service」 「sms」 「messaging」 「Opilo」 「Laravel SMS Driver」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 opilo/webservice 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 opilo/webservice 我们能提供哪些服务?
定制开发 / 二次开发

基于 opilo/webservice 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 415
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2015-12-08