打你一下

php华为云虚拟电话

<?php

namespace lib;

/**
* Description of Qrcode
*
* @author lsf
*/
class HuaweiyunPls {

public $recordDomain;
public $realUrl;
public $APP_KEY;
public $APP_SECRET;
public $headers;
public $fileName;

public function __construct($setting = array()) {
$this->realUrl = "https://rtcapi.cn-north-1.myhuaweicloud.com:12543/rest/caas/relationnumber/partners/v1.0";
$this->APP_KEY = $setting["huawei_key"];
$this->APP_SECRET = $setting["huawei_secret"];
$this->headers = [
'Accept: application/json',
'Content-Type: application/json;charset=UTF-8',
'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"',
'X-WSSE: ' . $this->buildWsseHeader($this->APP_KEY, $this->APP_SECRET)
];
}

/**
* 录音下载
* @param type $subscriptionId
* @param type $relationNum
*/
function downVideo($fileName = '1200_366_0_20161228102743.wav', $recordDomain = 'ostor.huawei.com') {
// 请求Body,可按需删除选填参数
$data = http_build_query([
'recordDomain' => $recordDomain,
'fileName' => $fileName
]);
$fullUrl = $this->realUrl . '?' . $data;
$context_options = [
'http' => [
'method' => 'GET', // 请求方法为DELETE
'header' => $this->headers,
'max_redirects' => '0', // 关闭重定向
'ignore_errors' => true // 获取错误码,方便调测
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
];
try {
$http_response_header = get_headers($fullUrl, 1, stream_context_create($context_options)); //获取响应消息头域信息
$response = file_get_contents($fullUrl, false, stream_context_create($context_options)); //获取响应消息数据信息
$http_response_header = json_decode($http_response_header, true);
if (strpos($http_response_header[0], '301') !== false) {
foreach ($http_response_header as $loop) {
if (strpos($loop, "Location") !== false) {
$fileUrl = trim(substr($loop, 10));
dump($fileUrl);
}
}
}
return array("success" => true, "message" => "");
} catch (\Exception $e) {
return array("success" => false, "message" => $e->getMessage());
}
}

/**
* AXB模式绑定信息查询接口
* @param type $subscriptionId
* @param type $relationNum
*/
function queryBindAxB($subscriptionId, $relationNum) {
// 请求Body,可按需删除选填参数
$data = http_build_query([
'subscriptionId' => $subscriptionId,
'relationNum' => $relationNum
]);
$fullUrl = $this->realUrl . '?' . $data;
$context_options = [
'http' => [
'method' => 'GET', // 请求方法为DELETE
'header' => $this->headers,
'ignore_errors' => true // 获取错误码,方便调测
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
];
try {
$response = file_get_contents($fullUrl, false, stream_context_create($context_options)); // 发送请求
$response = json_decode($response, true);
if ($response["resultcode"] != 0) {
return array("success" => false, "message" => $response["resultdesc"]);
}
return array("success" => true, "message" => $response);
} catch (\Exception $e) {
return array("success" => false, "message" => $e->getMessage());
}
}

/**
* 解绑ab号码
* @param type $subscriptionId
* @param type $relationNum
*/
//
function unBindAxB($subscriptionId, $relationNum) {
// 请求Body,可按需删除选填参数
$data = http_build_query([
'subscriptionId' => $subscriptionId,
'relationNum' => $relationNum
]);
$fullUrl = $this->realUrl . '?' . $data;
$context_options = [
'http' => [
'method' => 'DELETE', // 请求方法为DELETE
'header' => $this->headers,
'ignore_errors' => true // 获取错误码,方便调测
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
];
try {
$response = file_get_contents($fullUrl, false, stream_context_create($context_options)); // 发送请求
$response = json_decode($response, true);
if ($response["resultcode"] != 0) {
return array("success" => false, "message" => $response["resultdesc"]);
}
return array("success" => true, "message" => "");
} catch (\Exception $e) {
return array("success" => false, "message" => $e->getMessage());
}
}

/**
* ab号码绑定
*/
//"{"resultcode":"0","resultdesc":"Success","subscriptionId":"091e2255-20be-4ea1-8922-3bd58ed99b57","relationNum":"+8617120801049","callDirection":0,"duration":0,"maxDuration":0}"
function bindAxB($callerNum, $calleeNum, $relationNum) {
// 请求Body,可按需删除选填参数
$data = json_encode([
"relationNum" => $relationNum,
// 'areaCode' =>$this->areaCode,
'callerNum' => $callerNum,
'calleeNum' => $calleeNum,
// 'callDirection' => 0,
// 'duration' => 0.3,
'recordFlag' => true,
// 'recordHintTone' => $recordHintTone,
'maxDuration' => 0,
// 'lastMinVoice' => $lastMinVoice,
// 'privateSms' => $privateSms,
// 'preVoice' => $preVoice
]);
$context_options = [
'http' => [
'method' => 'POST', // 请求方法为POST
'header' => $this->headers,
'content' => $data,
'ignore_errors' => true // 获取错误码,方便调测
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
] // 为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题
];
try {
$response = file_get_contents($this->realUrl, false, stream_context_create($context_options)); // 发送请求
$response = json_decode($response, true);
if ($response["resultcode"] != 0) {
return array("success" => false, "message" => $response["resultdesc"]);
}
//subscriptionId绑定ID,唯一标识一组绑定关系。成功响应时必定返回。请记录该ID用于后续接口调用。
//relationNum绑定的X号码。
return array("success" => true, "message" => "", "data" => $response);
} catch (\Exception $e) {
return array("success" => false, "message" => $e->getMessage());
}
}

/**
* 构建X-WSSE值
*
* @param string $appKey
* @param string $appSecret
* @return string
*/
function buildWsseHeader($appKey, $appSecret) {
date_default_timezone_set("UTC");
$Created = date('Y-m-d\TH:i:s\Z'); //Created
$nonce = uniqid(); //Nonce
$base64 = base64_encode(hash('sha256', ($nonce . $Created . $appSecret), TRUE)); //PasswordDigest
return sprintf("UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"", $appKey, $base64, $nonce, $Created);
}

}

posted on 2020-06-27 15:50  打你一下  阅读(521)  评论(0编辑  收藏  举报

导航