php demo
采用curl网络请求方式
demo调用
<?php
require_once '../XsxxLoad.php';
$smsClient = new smsClient();
//单内容批量发送
$data['mobile'] = '13000000000';
$data['content'] = '【线上线下】您的验证码是1234';
$result = $smsClient->submit($data);
print_r($result);
SmsClient 针对接口的封装
<?php
require_once('HttpUtil.php');
class SmsClient
{
public $userId;
public $password;
public $url;
public $batch_url;
public $xsxx_config;
function __construct()
{
$this->xsxx_config = $GLOBALS['XSXX_CONFIG'];
$this->userId = $this->xsxx_config['USER_ID'];
$this->password = $this->xsxx_config['PASSWORD'];
$this->url = $this->xsxx_config['HOST'];
$this->batch_url = $this->xsxx_config['BATCH_HOST'];
}
//单内容多号码批量发送
public function submit($data = array())
{
$data['action'] = 'sendsms';
$data['userId'] = $this->userId;
$data['md5password'] = md5($this->password);
return HttpUtil::PostCURL($this->url, $data);
}
//多内容打包发送,不同的内容对应不同的手机号,并且一个内容只能发一个手机号
public function batch_submit($contentArr = array())
{
$data['userId'] = $this->userId;
$data['md5password'] = md5($this->password);
$data['contentArr'] = json_encode($contentArr);
return HttpUtil::PostCURL($this->batch_url, $data);
}
//获取短信状态反馈 一次最多反馈500条,反馈状态只推一次,拿过的状态不在平台保存
public function report($contentArr = array()){
$data['userId'] = $this->userId;
$data['md5password'] = md5($this->password);
$data['action'] = 'getsendreport';
return HttpUtil::PostCURL($this->url, $data);
}
//获取短信上行
public function deliver($contentArr = array()){
$data['userId'] = $this->userId;
$data['md5password'] = md5($this->password);
$data['action'] = 'getdeliver';
return HttpUtil::PostCURL($this->url, $data);
}
}
HttpUtil curl请求方式的封装
<?php
class HttpUtil
{
public static function PostCURL($url,$post_data){
$ch = curl_init();
/* 设置验证方式 */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));
/* 设置返回结果为流 */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* 设置超时时间*/
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
/* 设置通信方式 */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
$retry=0;
// 若执行失败则重试
do{
$output = curl_exec($ch);
$retry++;
// echo $retry."\n";
}while((curl_errno($ch) !== 0) && $retry<3);
if (curl_errno($ch) !== 0) {
$ret[status] = -1;
$ret[msg] = curl_error($ch);
curl_close($ch);
return $ret;
}
$output = trim($output, "\xEF\xBB\xBF");
$statusCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$ret = json_decode($output,true);
curl_close($ch);
return $ret;
}
}
config 基本配置
<?php
$xsxx_config = array();
$xsxx_config['HOST'] = 'http://127.0.0.1:8086/websms/smsJsonService';
$xsxx_config['BATCH_HOST'] = 'http://127.0.0.1:8086/batchwebsms/smsJsonService';
$xsxx_config['USER_ID'] = 'user03';
$xsxx_config['PASSWORD'] = '123456';
$GLOBALS['XSXX_CONFIG'] = $xsxx_config;
?>
XsxxLoad
<?php
require_once 'config.php';
function XsxxLoad($classname)
{
$filename = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib/'. $classname . '.php';
//echo $filename;
if (is_readable($filename)) {
require $filename;
}
}
if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
spl_autoload_register('XsxxLoad', true, true);
} else {
spl_autoload_register('XsxxLoad');
}
} else {
function __autoload($classname)
{
XsxxLoad($classname);
}
}
?>