百度开放云简单消息服务 SMS php接口源码 解决lack of param templateId
2015-07-06 10:06
861 查看
百度开放云的文档较不完善,参照百度BOS源码和SMS API文档,完成php SMS接口,发现返回lack of param templateId,后来再用java sdk调试发现JAVA程序content-type不含charset=utf-8,去掉后果然正常使用接口。
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/534b3472909a559c6703f7e74be0422d)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201507/81530c26f25f92265bc15c1fe6619130)
<?php
/**
* SMS接口类
*
*/
include 'BaiduBce.phar';
use BaiduBce\Auth\BceV1Signer;
use BaiduBce\Auth\SignOptions;
use BaiduBce\Bce;
use BaiduBce\BceClientConfigOptions;
use BaiduBce\BceBaseClient;
use BaiduBce\Exception\BceClientException;
use BaiduBce\Exception\BceServiceException;
use BaiduBce\Http\BceHttpClient;
use BaiduBce\Http\HttpHeaders;
use BaiduBce\Http\HttpContentTypes;
use BaiduBce\Http\HttpMethod;
use BaiduBce\Util\MimeTypes;
use BaiduBce\Util\HashUtils;
use BaiduBce\Util\HttpUtils;
use BaiduBce\Util\StringUtils;
use BaiduBce\Services\Bos\BosOptions;
class Sms extends BceBaseClient{
/**
* @var \BaiduBce\Auth\SignerInterface
*/
private $signer;
private $httpClient;
/**
*
*
* @param array $config 配置
*/
public function __construct() {
/* 配置 */
$config = array(
'credentials' => array(
'ak' => '',
'sk' => '',
),
'endpoint' => 'http://sms.bj.baidubce.com',
);
parent::__construct($config, 'sms');
$this->signer = new BceV1Signer();
$this->httpClient = new BceHttpClient();
}
public function sendsms($options= array(),$param=array()){
list($config) = $this->parseOptions($options, BosOptions::CONFIG);
return $this->sendRequest(
HttpMethod::POST,
array(BosOptions::CONFIG => $config,'body' =>'{"templateId":"'.$param['templateId'].'","receiver":["'.$param['receiver'].'"],"contentVar":"{\"code\":\"'.$param['code'].'\"}"}' )
);
}
/**
* Create HttpClient and send request
* @param string $httpMethod The Http request method
* @param array $varArgs The extra arguments
* @return mixed The Http response and headers.
*/
private function sendRequest($httpMethod, array $varArgs)
{
$defaultArgs = array(
BosOptions::CONFIG => array(),
'body' => null,
'headers' => array(),
'params' => array(),
'outputStream' => null,
'parseUserMetadata' => false
);
$args = array_merge($defaultArgs, $varArgs);
$config = $this->config;
if (!isset($args['headers'][HttpHeaders::CONTENT_TYPE])) {
$args['headers'][HttpHeaders::CONTENT_TYPE] ='application/json';
}
$response = $this->httpClient->sendRequest(
$config,
$httpMethod,
'/v1/message',
$args['body'],
$args['headers'],
$args['params'],
$this->signer,
$args['outputStream']
);
if ($args['outputStream'] === null) {
$result = $this->parseJsonResult($response['body']);
} else {
$result = new \stdClass();
}
$result->metadata =
$this->convertHttpHeadersToMetadata($response['headers']);
if ($args['parseUserMetadata']) {
$userMetadata = array();
foreach ($response['headers'] as $key => $value) {
if (StringUtils::startsWith($key, HttpHeaders::BCE_USER_METADATA_PREFIX)) {
$key = substr($key, strlen(HttpHeaders::BCE_USER_METADATA_PREFIX));
$userMetadata[urldecode($key)] = urldecode($value);
}
}
$result->metadata[BosOptions::USER_METADATA] = $userMetadata;
}
return $result;
}
}
<?php
/**
* SMS接口类
*
*/
include 'BaiduBce.phar';
use BaiduBce\Auth\BceV1Signer;
use BaiduBce\Auth\SignOptions;
use BaiduBce\Bce;
use BaiduBce\BceClientConfigOptions;
use BaiduBce\BceBaseClient;
use BaiduBce\Exception\BceClientException;
use BaiduBce\Exception\BceServiceException;
use BaiduBce\Http\BceHttpClient;
use BaiduBce\Http\HttpHeaders;
use BaiduBce\Http\HttpContentTypes;
use BaiduBce\Http\HttpMethod;
use BaiduBce\Util\MimeTypes;
use BaiduBce\Util\HashUtils;
use BaiduBce\Util\HttpUtils;
use BaiduBce\Util\StringUtils;
use BaiduBce\Services\Bos\BosOptions;
class Sms extends BceBaseClient{
/**
* @var \BaiduBce\Auth\SignerInterface
*/
private $signer;
private $httpClient;
/**
*
*
* @param array $config 配置
*/
public function __construct() {
/* 配置 */
$config = array(
'credentials' => array(
'ak' => '',
'sk' => '',
),
'endpoint' => 'http://sms.bj.baidubce.com',
);
parent::__construct($config, 'sms');
$this->signer = new BceV1Signer();
$this->httpClient = new BceHttpClient();
}
public function sendsms($options= array(),$param=array()){
list($config) = $this->parseOptions($options, BosOptions::CONFIG);
return $this->sendRequest(
HttpMethod::POST,
array(BosOptions::CONFIG => $config,'body' =>'{"templateId":"'.$param['templateId'].'","receiver":["'.$param['receiver'].'"],"contentVar":"{\"code\":\"'.$param['code'].'\"}"}' )
);
}
/**
* Create HttpClient and send request
* @param string $httpMethod The Http request method
* @param array $varArgs The extra arguments
* @return mixed The Http response and headers.
*/
private function sendRequest($httpMethod, array $varArgs)
{
$defaultArgs = array(
BosOptions::CONFIG => array(),
'body' => null,
'headers' => array(),
'params' => array(),
'outputStream' => null,
'parseUserMetadata' => false
);
$args = array_merge($defaultArgs, $varArgs);
$config = $this->config;
if (!isset($args['headers'][HttpHeaders::CONTENT_TYPE])) {
$args['headers'][HttpHeaders::CONTENT_TYPE] ='application/json';
}
$response = $this->httpClient->sendRequest(
$config,
$httpMethod,
'/v1/message',
$args['body'],
$args['headers'],
$args['params'],
$this->signer,
$args['outputStream']
);
if ($args['outputStream'] === null) {
$result = $this->parseJsonResult($response['body']);
} else {
$result = new \stdClass();
}
$result->metadata =
$this->convertHttpHeadersToMetadata($response['headers']);
if ($args['parseUserMetadata']) {
$userMetadata = array();
foreach ($response['headers'] as $key => $value) {
if (StringUtils::startsWith($key, HttpHeaders::BCE_USER_METADATA_PREFIX)) {
$key = substr($key, strlen(HttpHeaders::BCE_USER_METADATA_PREFIX));
$userMetadata[urldecode($key)] = urldecode($value);
}
}
$result->metadata[BosOptions::USER_METADATA] = $userMetadata;
}
return $result;
}
}
相关文章推荐
- 一个关于if else容易迷惑的问题
- Android Manifest 用法
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 房产界已上市和IPO路上的难兄难弟:房天下与房多多
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- 利用百度地图提供的API做的应用
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- Android ADT 23.0.0无法更新到23.0.2问题解决方案
- 百度20年:搜索帝国的崛起、式微与重生
- PHP数据库长连接mysql_pconnect的细节
- 小白观察:Google 开始清除百度旗下公司开发的 46 款应用程序
- Php Installing An Expansion
- 27 个全方位的开放式教育解决方案
- SDKMAN:轻松管理多个软件开发套件 (SDK) 的命令行工具
- 百度全面恢复网站权重,流量暴涨中
- 一个小型js框架myJSFrame附API使用帮助