【发布时间】:2016-09-20 19:46:11
【问题描述】:
我正在测试 Clickatell API 以在我的基于 php 的应用程序中集成 SMS 确认,我使用他们的 rest api 将消息发送给自己进行测试,但消息永远不会到达。
我的尝试
我用这个https://www.clickatell.com/developers/api-docs/get-coverage-rest/ 来检查覆盖率,这是 JSON 响应:
object(stdClass)[54]
public 'data' =>
object(stdClass)[57]
public 'routable' => boolean true
public 'destination' => string ' 21655609125' (length=12)
public 'minimumCharge' => float 0.8
我还通过检查状态来确保消息确实是发送的;这是 JSON 响应:
object(stdClass)[54]
public 'data' =>
object(stdClass)[57]
public 'charge' => float 0.8
public 'messageStatus' => string '004' (length=3)
public 'description' => string 'Received by recipient' (length=21)
public 'apiMessageId' => string 'b57f4a28dece65a134b56be2010c8a78' (length=32)
public 'clientMessageId' => string '' (length=0)
然后我尝试了他们自己的网站发送消息报告,这就是我所看到的:
消息内容 感谢您测试 Clickatell 的网关覆盖范围。你 将能够在您初始后更改您的消息内容 购买消息积分。
突尼斯移动网络:Orange To 21655609125
收件人已收到(状态 4)
但我自己从来没有收到过消息。可能是什么问题?
编辑:这是我目前在我的应用中使用的完整类
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* MC_SMS class
*/
class MC_SMS {
public $rest_uri = 'https://api.clickatell.com/rest';
public $method = 'post';
public $args = array();
/**
* Constructor
*/
public function __construct( $action, $data = null ) {
$this->data = $data;
$this->init();
switch( $action ) :
// Send message
case 'send' :
$this->endpoint = '/message';
$this->method = 'post';
break;
// Message status
case 'status' :
$this->endpoint = '/message/' . $data;
$this->method = 'get';
break;
// Network coverage
case 'coverage' :
$this->endpoint = '/coverage/' . $data;
$this->method = 'get';
break;
// Account balance
case 'balance' :
$this->endpoint = '/account/balance';
$this->method = 'get';
break;
endswitch;
$this->queried_uri = $this->rest_uri . $this->endpoint;
$this->do_request();
$this->response = ( isset( $this->response_body['body'] ) ) ? json_decode( $this->response_body['body'] ) : null;
}
/**
* Init.
*/
public function init() {
$this->headers = array(
'X-Version' => 1,
'Authorization' => 'Bearer ClHrbIEo_LwAlSVTSMemBIA5Gmvz8HNb5sio3N9GVDdAO_PPJPaZKzdi8Y8cDSmrs4A4',
'Content-Type' => 'application/json',
'Accept' => 'application/json'
);
$this->data = ( ! empty( $this->data ) && is_array( $this->data ) ) ? json_encode( $this->data ) : null;
$this->args['headers'] = $this->headers;
if ( $this->data ) {
$this->args['body'] = $this->data;
}
}
/**
* Do the API request
*/
public function do_request() {
if ( $this->method == 'get' ) {
$this->response_body = wp_remote_get( $this->queried_uri, $this->args );
}
if ( $this->method == 'post' ) {
$this->response_body = wp_remote_post( $this->queried_uri, $this->args );
}
}
}
【问题讨论】:
-
你付过账吗?
-
还没有 :) 我只是用测试积分对其进行测试,以确定它是否适用于我的应用程序。
-
贴出你实际用来发送消息的代码
-
我发布了处理所有请求的整个课程。我这样做是为了发送一条消息: $msg = new SMS( 'send', array( 'to' => array( 'mynumber' ), 'text' => 'test msg' ) );
标签: php api clickatell