定制 rockandscissor/clockworksms 二次开发

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

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

rockandscissor/clockworksms

Composer 安装命令:

composer require rockandscissor/clockworksms

包简介

ClockworkSMS, International SMS API

关键字:

README 文档

README

This wrapper lets you interact with Clockwork without the hassle of having to create any XML or make HTTP calls. This version of the wrapper has been forked and updated to work with newer versions of PHP by Rock & Scissor as Mediaburst was acquired by Textanywhere which means the original repo was abandoned. We intend to keep this package up to date as long as the API is still supported by Textanywhere.

What's Clockwork?

Clockwork is Mediaburst's SMS API.

Prerequisites

Usage

Installing with composer

The easiest way to get Clockwork is to use Composer to automatically download and include it in your project. Setup Composer and add us to your composer.json

{
    "require": {
        "rockandscissor/clockworksms": "2.0.*"
    }
}

If you are using your own autoloader we are using the PSR-4 namespacing scheme.

Including directly

Download the Clockwork library, put it in your project and require the Clockwork class and the ClockworkException classes:

require 'src/Clockwork.php';
require 'src/ClockworkException.php';

Sending a message

$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY ); //Be careful not to post your API Keys to public repositories.
$message = array( 'to' => '441234567891', 'message' => 'This is a test!' );
$result = $clockwork->send( $message );

Sending multiple messages

We recommend you use batch sizes of 500 messages or fewer. By limiting the batch size it prevents any timeouts when sending.

$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY ); //Be careful not to post your API Keys to public repositories.
$messages = array(
    array( 'to' => '441234567891', 'message' => 'This is a test!' ),
    array( 'to' => '441234567892', 'message' => 'This is a test 2!' )
);
$results = $clockwork->send( $messages );

Handling the response

The responses come back as arrays, these contain the unique Clockwork message ID, whether the message worked (success), and the original SMS so you can update your database.

Array
(
    [id] => VE_164732148
    [success] => 1
    [sms] => Array
        (
            [to] => 441234567891
            [message] => This is a test!
        )

)

If you send multiple SMS messages in a single send, you'll get back an array of results, one per SMS.

The result will look something like this:

Array
(
    [0] => Array
        (
            [id] => VI_143228951
            [success] => 1
            [sms] => Array
                (
                    [to] => 441234567891
                    [message] => This is a test!
                )

        )

    [1] => Array
        (
            [id] => VI_143228952
            [success] => 1
            [sms] => Array
                (
                    [to] => 441234567892
                    [message] => This is a test 2!
                )

        )

)

If a message fails, the reason for failure will be set in error_code and error_message.

For example, if you send to invalid phone number "abc":

Array
(
    [error_code] => 10
    [error_message] => Invalid 'To' Parameter
    [success] => 0
    [sms] => Array
        (
            [to] => abc
            [message] => This is a test!
        )

)

Checking your balance

Check your available SMS balance:

$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY ); //Be careful not to post your API Keys to public repositories.
$clockwork->checkBalance();

This will return:

Array
(
    [symbol] => £
    [balance] => 351.91
    [code] => GBP
    [account_type] => PAYG
)

Account Type can be either PAYG or Invoice.

Handling Errors

The Clockwork wrapper will throw a ClockworkException if the entire call failed.

try
{
    $clockwork = new rockandscissor\ClockworkSMS\Clockwork( 'invalid_key' );
    $message = array( 'to' => 'abc', 'message' => 'This is a test!' );
    $result = $clockwork->send( $message );
}
catch( mediaburst\ClockworkSMS\ClockworkException $e )
{
    print $e->getMessage();
    // Invalid API Key
}

Advanced Usage

This class has a few additional features that some users may find useful, if these are not set your account defaults will be used.

Optional Parameters

See the Clockwork Documentation for full details on these options.

  • $from [string]

    The from address displayed on a phone when they receive a message

  • $long [boolean]

    Enable long SMS. A standard text can contain 160 characters, a long SMS supports up to 459.

  • $truncate [nullable boolean]

    Truncate the message payload if it is too long, if this is set to false, the message will fail if it is too long.

  • $invalid_char_action [string]

    What to do if the message contains an invalid character. Possible values are

    • error - Fail the message
    • remove - Remove the invalid characters then send
    • replace - Replace some common invalid characters such as replacing curved quotes with straight quotes
  • $ssl [boolean, default: true]

    Use SSL when making an HTTP request to the Clockwork API

Setting Options

Global Options

Options set on the API object will apply to all SMS messages unless specifically overridden.

In this example both messages will be sent from Clockwork:

$options = array( 'from' => 'Clockwork' );
$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY, $options ); //Be careful not to post your API Keys to public repositories.
$messages = array(
    array( 'to' => '441234567891', 'message' => 'This is a test!' ),
    array( 'to' => '441234567892', 'message' => 'This is a test 2!' )
);
$results = $clockwork->send( $messages );

Per-message Options

Set option values individually on each message.

In this example, one message will be from Clockwork and the other from 84433:

$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY, $options ); //Be careful not to post your API Keys to public repositories.
$messages = array(
    array( 'to' => '441234567891', 'message' => 'This is a test!', 'from' => 'Clockwork' ),
    array( 'to' => '441234567892', 'message' => 'This is a test 2!', 'from' => '84433' )
);
$results = $clockwork->send( $messages );

SSL Errors

Due to the huge variety of PHP setups out there a small proportion of users may get PHP errors when making API calls due to their SSL configuration.

The errors will generally look something like this:

Fatal error:
Uncaught exception 'Exception' with message 'HTTP Error calling Clockwork API
HTTP Status: 0
cURL Erorr: SSL certificate problem, verify that the CA cert is OK.
Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed'

If you're seeing this error there are two fixes available, the first is easy, simply disable SSL on Clockwork calls. Alternatively you can setup your PHP install with the correct root certificates.

Disable SSL on Clockwork calls

$options = array( 'ssl' => false );
$clockwork = new mediaburst\ClockworkSMS\Clockwork( $API_KEY, $options );  //Be careful not to post your API Keys to public repositories.

Setup SSL root certificates on your server

This is much more complicated as it depends on your setup, however there are many guides available online. Try a search term like "windows php curl root certificates" or "ubuntu update root certificates".

License

This project is licensed under the MIT open-source license.

A copy of this license can be found in license.txt.

Contributing

If you have any feedback on this wrapper drop us an email to hello@clockworksms.com.

The project is hosted on GitHub at https://github.com/mediaburst/clockwork-php. If you would like to contribute a bug fix or improvement please fork the project and submit a pull request.

rockandscissor/clockworksms 适用场景与选型建议

rockandscissor/clockworksms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 39
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-29