转绑
更新时间:2024-03-15
接口描述
AXB转绑接口可以实现在已有的AXB绑定关系中更换B号码,在使用该接口前需要明确AXB三者角色,比如在百度打车的场景中,A可以是用户的联系方式,X是中间隐私号码,B可以是出租车司机的联系方式,如果这一单B司机接单了,因为意外情况无法走完这单,B2就会走换绑接口,通过X联系到客户,走完这单。
请求结构
POST /cloud/api/v{version}/axb/changeBinding HTTP/1.1
Host: pns.baidubce.com
Content-Type: application/json; charset=utf-8
x-bce-date: 2019-10-30T10:55:26Z
Authorization: authorization string
{
"telA": "A号码",
"telX": "X号码",
"telB": "B号码",
"areaCode": "需要X号码所属区号",
"bindId": "要转绑已有axb绑定关系的bindid"
}
请求域名
参考API文档中的服务域名
请求头域
除公共头域外,无其它特殊头域。
请求参数
参数名称 | 类型 | 是否必须 | 参数位置 | 描述 |
---|---|---|---|---|
version | String | 是 | URL参数 | API版本号,参考通用说明中的API版本号,目前为1 |
telA | String | 是 | RequestBody参数 | 主叫号码 格式:手机或固话座机 |
telB | String | 是 | RequestBody参数 | 被叫号码 格式:手机或固话座机 |
telX | String | 是 | RequestBody参数 | 中间X号码,不传平台自动分配申请的X号码 |
areaCode | String | 是 | RequestBody参数 | 需要的 X 号码所属于区号 |
bindId | Json | 是 | RequestBody参数 | 要转绑已有axb绑定关系的bindid |
响应头域
除了公共头域外,如果发生网关错误,可能头域中存在以下特殊头域:
参数名 | 类型 | 说明 |
---|---|---|
X-Bce-Error-Code | String | 表示异常情况下的错误码 |
X-Bce-Error-Message | String | 表示异常情况下的错误信息 |
响应参数
参数名称 | 类型 | 描述 |
---|---|---|
code | String | 平台返回的错误码,0表示成功 |
msg | String | 平台返回的错误消息 |
data | Json | 平台返回的响应数据 |
绑定操作成功后的响应数据data:
参数名称 | 类型 | 描述 |
---|---|---|
bindId | String | AXB绑定ID |
telX | String | AXB绑定X号码 |
错误码
请参照前文的网关错误码和平台错误码。
请求示例
POST /cloud/api/v{version}/axb/changeBinding HTTP/1.1
Host: pns.baidubce.com
Content-Type: application/json; charset=utf-8
x-bce-date: 2019-10-30T10:55:26Z
Authorization: bce-auth-v1/318857a8f08b11e9845ca7e54775a0c2/2019-10-17T03:07:21Z/1800/host/212eef8cfe1ac94be56c4afedb9360bba621ef646c3c8288971d36801d70501a
{
"telA": "17600901081",
"telX": "17801483021",
"telB": "13911528327",
"areaCode": "10",
"bindId": "4056109308766826953"
}
PHP代码示例
<?php
function gmtime()
{
return (time() - date('Z'));
}
function debug($k,$v){
echo "\n======={$k}======\n";
var_export($v);
echo "\n=====end====\n";
}
/**
* Http Client
*/
class AipHttpClient{
/**
* HttpClient
* @param array $headers HTTP header
*/
public function __construct($headers=array()){
$this->headers = $this->buildHeaders($headers);
$this->connectTimeout = 60000;
$this->socketTimeout = 60000;
$this->conf = array();
}
/**
* 连接超时
* @param int $ms 毫秒
*/
public function setConnectionTimeoutInMillis($ms){
$this->connectTimeout = $ms;
}
/**
* 响应超时
* @param int $ms 毫秒
*/
public function setSocketTimeoutInMillis($ms){
$this->socketTimeout = $ms;
}
/**
* 配置
* @param array $conf
*/
public function setConf($conf){
$this->conf = $conf;
}
/**
* 请求预处理
* @param resource $ch
*/
public function prepare($ch){
foreach($this->conf as $key => $value){
curl_setopt($ch, $key, $value);
}
}
/**
* @param string $url
* @param array $data HTTP POST BODY
* @param array $param HTTP URL
* @param array $headers HTTP header
* @return array
*/
public function post($url, $data=array(), $params=array(), $headers=array()){
$url = $this->buildUrl($url, $params);
$headers = array_merge($this->headers, $this->buildHeaders($headers));
debug('headers',$headers);
debug('body',$data);
$ch = curl_init();
$this->prepare($ch);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
echo "Curl options:\n";
var_dump(curl_getinfo($ch));
$content = curl_exec($ch);
echo "Response content:\n";
var_dump($content); // 输出返回的内容
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($code === 0){
throw new Exception(curl_error($ch));
}
curl_close($ch);
return array(
'code' => $code,
'content' => $content,
);
}
/**
* @param string $url
* @param array $param HTTP URL
* @param array $headers HTTP header
* @return array
*/
public function get($url, $params=array(), $headers=array()){
$url = $this->buildUrl($url, $params);
$headers = array_merge($this->headers, $this->buildHeaders($headers));
$ch = curl_init();
$this->prepare($ch);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
$content = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($code === 0){
throw new Exception(curl_error($ch));
}
curl_close($ch);
return array(
'code' => $code,
'content' => $content,
);
}
/**
* 构造 header
* @param array $headers
* @return array
*/
private function buildHeaders($headers){
$result = array();
foreach($headers as $k => $v){
$result[] = sprintf('%s:%s', $k, $v);
}
return $result;
}
/**
*
* @param string $url
* @param array $params 参数
* @return string
*/
private function buildUrl($url, $params){
if(!empty($params)){
$str = http_build_query($params);
return $url . (strpos($url, '?') === false ? '?' : '&') . $str;
}else{
return $url;
}
}
}
/**
*
*/
class BaiDuYun
{
/**
* $conf=['access'=>'xxx','secret'=>'xxx'] https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
*
*
* @method __construct
*
* @param array $conf [description]
*/
function __construct($conf=[])
{
$this->conf=$conf;
$this->client= new AipHttpClient([
'Content-Type'=>'application/json; charset=utf-8',
]);
$this->addtionHeaders=[ 'x-bce-date'=> date('Y-m-d\TH:i:s\Z',gmtime())];
$this->version=1;
$this->APIBASE="https://pns.baidubce.com";
}
private function auth($Method,$api){
return $this->authStringPrefix()."/host;x-bce-date/".$this->Signature($Method,$api);
}
private function Signature($Method,$api){
$c=$this->CanonicalRequest($Method,$api);
$sk=$this->SigningKey();
debug("CanonicalRequest",$c);
debug("SigningKey",$sk);
return self::sha256($sk,$c);
}
private function SigningKey(){
$s= self::sha256($this->conf['secret'],trim($this->authStringPrefix()));
return $s;
}
static function sha256($a,$b){
return hash_hmac('sha256',$b,$a);
}
private function authStringPrefix(){
$t= date('Y-m-d\TH:i:s\Z',gmtime());
// $t='2019-12-03T18:03:23Z';
return "bce-auth-v1/{$this->conf['access']}/{$t}/1800";
}
private function CanonicalRequest($Method,$api){
$data[]=$Method;
$u=parse_url($api);
$data[]=str_replace('%2F','/',urlencode($u['path']));
if(isset($u['query'])){
parse_str($u['query'],$qs);
ksort($qs);
foreach ($qs as $k => $v) {
$q[]=urlencode($k).'='.urlencode($v);
}
$data[]= implode("&", $q);
}else{
$data[]='';
}
$a=["host:".$u['host'],];
foreach ( $this->addtionHeaders as $k => $v) {
$a[]="{$k}:".urlencode($v);
}
$data[]=implode("\n", $a);
return implode("\n", $data);
}
public function Post($api,$data=null){
$api=$this->APIBASE.$api;
var_dump($api);
$this->addtionHeaders["Authorization"]=$this->auth("POST",$api);
$res=$this->client->post($api,$data?json_encode($data):null,[],$this->addtionHeaders);
if($res['code']!=200){
throw new \Exception($res['content'], $res['code']);
}
return json_decode( $res['content'],1);
}
/**
* [ChangeBinding description] 百度云PNS-AXB模式转绑接口
*
* @method ChangeBinding
* @param telA String 是 RequestBody参数 主叫号码 格式:手机或固话座机
* @param telB String 是 RequestBody参数 被叫号码 格式:手机或固话座机
* @param telX String 是 RequestBody参数 中间X号码,不传平台自动分配申请的X号码
* @param areaCode String 是 RequestBody参数 需要的 X 号码所属于区号
* @param bindId Json 是 RequestBody参数 要转绑已有axb绑定关系的bindid
**/
public function ChangeBinding($tela,$telb,$telx="",$areaCode="10",$bindId=''){
$api="/cloud/api/v{$this->version}/axb/changeBinding";
$data=[
"telA"=>$tela,
"telX"=>$telx,
"telB"=>$telb,
"areaCode"=>$areaCode,
"bindId"=>$bindId
];
if($telx){
$data['telX']= $telx;
}
return $this->Post($api,$data);
}
//demo access: 用户ak信息 secret: 用户sk信息
$conf=['access'=>'您的ak','secret'=>'您的sk'];
$bd = new BaiDuYun($conf);
//$bd->ChangeBinding("18011112222","18911112222");
//$bd->ChangeBinding("18620084369"); // 转绑
响应示例
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Date: Wed, 08 Jul 2019 03:28:11 GMT
x-bce-request-id: d8752367-38e8-45e4-b4c7-e53be3137ce5
X-Bce-Gateway-Region: BJ
{
"code":"0",
"msg":"成功",
"data":{
"bindId":"",
"telX":""
}
}