pmill / clockwork-php
This package is not auto-updated.
Last update: 2024-09-14 18:04:20 UTC
README
此包装器让您无需创建任何XML或进行HTTP调用即可与Clockwork交互。
什么是Clockwork?
Clockwork是MediaBurst的SMS API。
先决条件
- 一个Clockwork账户
使用方法
需要Clockwork库
require 'class-Clockwork.php';
发送消息
$clockwork = new Clockwork( $API_KEY ); $message = array( 'to' => '441234567891', 'message' => 'This is a test!' ); $result = $clockwork->send( $message );
发送多条消息
我们建议您使用500条或更少的批量大小。通过限制批量大小可以防止发送时超时。
$clockwork = new Clockwork( $API_KEY ); $messages = array( array( 'to' => '441234567891', 'message' => 'This is a test!' ), array( 'to' => '441234567892', 'message' => 'This is a test 2!' ) ); $results = $clockwork->send( $messages );
处理响应
响应以数组形式返回,这些数组包含唯一的Clockwork消息ID、消息是否成功(success
)以及原始短信,以便您更新数据库。
Array
(
[id] => VE_164732148
[success] => 1
[sms] => Array
(
[to] => 441234567891
[message] => This is a test!
)
)
如果您在一次发送中发送多条短信,您将得到一个结果数组,每条短信一个。
结果看起来可能像这样
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!
)
)
)
如果消息失败,失败原因将设置在error_code
和error_message
中。
例如,如果您向无效的手机号码"abc"发送消息
Array
(
[error_code] => 10
[error_message] => Invalid 'To' Parameter
[success] => 0
[sms] => Array
(
[to] => abc
[message] => This is a test!
)
)
检查余额
检查您的可用短信余额
$clockwork = new Clockwork( $API_KEY ); $clockwork->checkBalance();
这将返回
Array
(
[symbol] => £
[balance] => 351.91
[code] => GBP
)
错误处理
如果整个调用失败,Clockwork包装器将抛出ClockworkException
。
try { $clockwork = new Clockwork( 'invalid_key' ); $message = array( 'to' => 'abc', 'message' => 'This is a test!' ); $result = $clockwork->send( $message ); } catch( ClockworkException $e ) { print $e->getMessage(); // Invalid API Key }
高级使用
此类有一些用户可能发现有用的附加功能,如果未设置这些选项,则将使用账户默认值。
可选参数
有关这些选项的完整详细信息,请参阅Clockwork文档。
-
$from [字符串]
当接收短信时在手机上显示的发件人地址
-
$long [布尔值]
启用长短信。标准短信可包含160个字符,长短信支持最多459个字符。
-
$truncate [可为空的布尔值]
如果消息负载过长,则截断消息。如果此选项设置为false,则如果消息过长,消息将失败。
-
$invalid_char_action [字符串]
如果消息包含无效字符时将执行的操作。可能的值包括
- error - 失败消息
- remove - 删除无效字符然后发送
- replace - 用直引号替换一些常见的无效字符,例如用直引号替换曲线引号
-
$ssl [布尔值,默认: true]
在向Clockwork API发起HTTP请求时使用SSL
设置选项
全局选项
在API对象上设置的选项将应用于所有短信消息,除非明确覆盖。
在此示例中,两条消息都将从Clockwork发送
$options = array( 'from' => 'Clockwork' ); $clockwork = new Clockwork( $API_KEY, $options ); $messages = array( array( 'to' => '441234567891', 'message' => 'This is a test!' ), array( 'to' => '441234567892', 'message' => 'This is a test 2!' ) ); $results = $clockwork->send( $messages );
每条消息的选项
在每条消息上单独设置选项值。
在此示例中,一条消息来自Clockwork,另一条来自84433
$clockwork = new Clockwork( $API_KEY, $options ); $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错误
由于PHP配置的多样性很大,一小部分用户在调用API时可能会因SSL配置而遇到PHP错误。
错误通常看起来像这样
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'
如果您看到此错误,有两个修复方案可用,第一个是简单的,只需在Clockwork调用中禁用SSL。或者,您可以选择在您的PHP安装中设置正确的根证书。
在Clockwork调用中禁用SSL
$options = array( 'ssl' => false ); $clockwork = new Clockwork( $API_KEY, $options );
在您的服务器上设置SSL根证书
这要复杂得多,因为它取决于您的配置,但网上有很多指南可用。尝试搜索关键词“windows php curl 根证书”或“ubuntu 更新根证书”。
许可证
本项目采用ISC开源许可证。
本许可证的副本可在license.txt文件中找到。
贡献
如果您对这款包装有任何反馈,请发送电子邮件至 hello@clockworksms.com。
本项目托管在GitHub上,网址为 https://github.com/mediaburst/clockwork-php。如果您想贡献错误修复或改进,请克隆项目并提交一个拉取请求。