初始化仓库

This commit is contained in:
wangxiaowei
2025-04-22 14:09:52 +08:00
commit 8b100110bb
5155 changed files with 664201 additions and 0 deletions

View File

@ -0,0 +1,593 @@
<?php
/*
* Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace TencentCloud\Common;
use \ReflectionClass;
use TencentCloud\Common\Http\HttpConnection;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Profile\RegionBreakerProfile;
use TencentCloud\Common\CircuitBreaker;
use TencentCloud\Common\Exception\TencentCloudSDKException;
/**
* 抽象api类禁止client引用
* @package TencentCloud\Common
*/
abstract class AbstractClient
{
/**
* @var string SDK版本
*/
public static $SDK_VERSION = "SDK_PHP_3.0.990";
/**
* @var integer http响应码200
*/
public static $HTTP_RSP_OK = 200;
private $PHP_VERSION_MINIMUM = "5.6.0";
/**
* @var Credential 认证类实例,保存认证相关字段
*/
private $credential;
/**
* @var ClientProfile 会话配置信息类
*/
private $profile;
private $regionBreakerProfile;
private $circuitBreaker;
/**
* @var string 产品地域
*/
private $region;
/**
* @var string 请求路径
*/
private $path;
/**
* @var string sdk版本号
*/
private $sdkVersion;
/**
* @var string api版本号
*/
private $apiVersion;
/**
* @var HttpConnection
*/
private $httpConn;
/**
* 基础client类
* @param string $endpoint Deprecated, we use service+rootdomain instead
* @param string $version api版本
* @param Credential $credential 认证信息实例
* @param string $region 产品地域
* @param ClientProfile $profile
*/
function __construct($endpoint, $version, $credential, $region, $profile=null)
{
$this->path = "/";
$this->credential = $credential;
$this->region = $region;
if ($profile) {
$this->profile = $profile;
} else {
$this->profile = new ClientProfile();
}
$this->getRefreshedEndpoint();
$this->sdkVersion = AbstractClient::$SDK_VERSION;
$this->apiVersion = $version;
if ($this->profile->enableRegionBreaker) {
if (is_null($this->profile->getRegionBreakerProfile())) {
throw new TencentCloudSDKException("ClientError", "RegionBreakerProfile have not been set yet.");
}
$this->circuitBreaker = new CircuitBreaker($this->profile->getRegionBreakerProfile());
}
if (version_compare(phpversion(), $this->PHP_VERSION_MINIMUM, '<') && $profile->getCheckPHPVersion()) {
throw new TencentCloudSDKException("ClientError", "PHP version must >= ".$this->PHP_VERSION_MINIMUM.", your current is ".phpversion());
}
$this->httpConn = $this->createConnect();
}
/**
* 设置产品地域
* @param string $region 地域
*/
public function setRegion($region)
{
$this->region = $region;
}
/**
* 获取产品地域
* @return string
*/
public function getRegion()
{
return $this->region;
}
/**
* 设置认证信息实例
* @param Credential $credential 认证信息实例
*/
public function setCredential($credential)
{
$this->credential = $credential;
}
/**
* 返回认证信息实例
* @return Credential
*/
public function getCredential()
{
return $this->credential;
}
/**
* 设置配置实例
* @param ClientProfile $profile 配置实例
*/
public function setClientProfile($profile)
{
$this->profile = $profile;
}
/**
* 返回配置实例
* @return ClientProfile
*/
public function getClientProfile()
{
return $this->profile;
}
/**
* @param string $action 方法名
* @param array $request 参数列表
* @return mixed
* @throws TencentCloudSDKException
*/
public function __call($action, $request)
{
if ($this->profile->enableRegionBreaker) {
return $this->doRequestWithOptionsOnRegionBreaker($action, $request[0], array());
}
else {
return $this->doRequestWithOptions($action, $request[0], array());
}
}
/**
* @param string $action
* @param array $headers
* @param string $body json content
* @return mixed
* @throws TencentCloudSDKException
*/
public function callJson($action, $body, $headers=null) {
try {
$responseData = null;
$options = array(
"IsCommonJson" => true,
);
if ($this->profile->getHttpProfile()->getReqMethod() == HttpProfile::$REQ_GET) {
throw new TencentCloudSDKException("ClientError", "Common client call doesn't support GET method");
}
if ($this->profile->getSignMethod() != ClientProfile::$SIGN_TC3_SHA256) {
throw new TencentCloudSDKException("ClientError", "Common client call must use TC3-HMAC-SHA256");
}
$responseData = $this->doRequestWithTC3($action, $body, $options, $headers, null);
if ($responseData->getStatusCode() !== AbstractClient::$HTTP_RSP_OK) {
throw new TencentCloudSDKException($responseData->getReasonPhrase(), $responseData->getBody());
}
$resp = json_decode($responseData->getBody(), true)["Response"];
if (array_key_exists("Error", $resp)) {
throw new TencentCloudSDKException($resp["Error"]["Code"], $resp["Error"]["Message"], $resp["RequestId"]);
}
return $resp;
} catch (\Exception $e) {
if (!($e instanceof TencentCloudSDKException)) {
throw new TencentCloudSDKException("", $e->getMessage());
} else {
throw $e;
}
}
}
/**
* @param string $action 方法名
* @param array $headers 自定义headers
* @param string $body content
* @return mixed
* @throws TencentCloudSDKException
*/
public function call_octet_stream($action, $headers, $body) {
try {
$responseData = null;
$options = array(
"IsOctetStream" => true,
);
switch ($this->profile->getSignMethod()) {
case ClientProfile::$SIGN_TC3_SHA256:
$responseData = $this->doRequestWithTC3($action, Null, $options, $headers, $body);
break;
default:
throw new TencentCloudSDKException("ClientError", "Invalid sign method");
break;
}
if ($responseData->getStatusCode() !== AbstractClient::$HTTP_RSP_OK) {
throw new TencentCloudSDKException($responseData->getReasonPhrase(), $responseData->getBody());
}
$tmpResp = json_decode($responseData->getBody(), true)["Response"];
if (array_key_exists("Error", $tmpResp)) {
throw new TencentCloudSDKException($tmpResp["Error"]["Code"], $tmpResp["Error"]["Message"], $tmpResp["RequestId"]);
}
return $this->returnResponse($action, $tmpResp);
} catch (\Exception $e) {
if (!($e instanceof TencentCloudSDKException)) {
throw new TencentCloudSDKException("", $e->getMessage());
} else {
throw $e;
}
}
}
protected function doRequestWithOptions($action, $request, $options)
{
try {
$responseData = null;
$serializeRequest = $request->serialize();
$method = $this->getPrivateMethod($request, "arrayMerge");
$serializeRequest = $method->invoke($request, $serializeRequest);
switch ($this->profile->getSignMethod()) {
case ClientProfile::$SIGN_HMAC_SHA1:
case ClientProfile::$SIGN_HMAC_SHA256:
$responseData = $this->doRequest($action, $serializeRequest);
break;
case ClientProfile::$SIGN_TC3_SHA256:
$responseData = $this->doRequestWithTC3($action, $request, $options, array(), "");
break;
default:
throw new TencentCloudSDKException("ClientError", "Invalid sign method");
break;
}
if ($responseData->getStatusCode() !== AbstractClient::$HTTP_RSP_OK) {
throw new TencentCloudSDKException($responseData->getReasonPhrase(), $responseData->getBody());
}
$tmpResp = json_decode($responseData->getBody(), true)["Response"];
if (array_key_exists("Error", $tmpResp)) {
throw new TencentCloudSDKException($tmpResp["Error"]["Code"], $tmpResp["Error"]["Message"], $tmpResp["RequestId"]);
}
return $this->returnResponse($action, $tmpResp);
} catch (\Exception $e) {
if (!($e instanceof TencentCloudSDKException)) {
throw new TencentCloudSDKException("", $e->getMessage());
} else {
throw $e;
}
}
}
protected function doRequestWithOptionsOnRegionBreaker($action, $request, $options)
{
try {
$endpoint = $this->profile->getRegionBreakerProfile()->masterEndpoint;
list($generation, $need_break) = $this->circuitBreaker->beforeRequests();
if ($need_break) {
$endpoint = $this->profile->getRegionBreakerProfile()->slaveEndpoint;
}
$this->profile->getHttpProfile()->setEndpoint($endpoint);
$responseData = null;
$serializeRequest = $request->serialize();
$method = $this->getPrivateMethod($request, "arrayMerge");
$serializeRequest = $method->invoke($request, $serializeRequest);
switch ($this->profile->getSignMethod()) {
case ClientProfile::$SIGN_HMAC_SHA1:
case ClientProfile::$SIGN_HMAC_SHA256:
$responseData = $this->doRequest($action, $serializeRequest);
break;
case ClientProfile::$SIGN_TC3_SHA256:
$responseData = $this->doRequestWithTC3($action, $request, $options, array(), "");
break;
default:
throw new TencentCloudSDKException("ClientError", "Invalid sign method");
break;
}
if ($responseData->getStatusCode() !== AbstractClient::$HTTP_RSP_OK) {
throw new TencentCloudSDKException($responseData->getReasonPhrase(), $responseData->getBody());
}
$tmpResp = json_decode($responseData->getBody(), true)["Response"];
if (array_key_exists("Error", $tmpResp)) {
$this->circuitBreaker->afterRequests($generation, True);
throw new TencentCloudSDKException($tmpResp["Error"]["Code"], $tmpResp["Error"]["Message"], $tmpResp["RequestId"]);
}
$this->circuitBreaker->afterRequests($generation, True);
return $this->returnResponse($action, $tmpResp);
} catch (\Exception $e) {
$this->circuitBreaker->afterRequests($generation, False);
if (!($e instanceof TencentCloudSDKException)) {
throw new TencentCloudSDKException("", $e->getMessage());
} else {
throw $e;
}
}
}
private function doRequest($action, $request)
{
switch ($this->profile->getHttpProfile()->getReqMethod()) {
case HttpProfile::$REQ_GET:
return $this->getRequest($action, $request);
break;
case HttpProfile::$REQ_POST:
return $this->postRequest($action, $request);
break;
default:
throw new TencentCloudSDKException("", "Method only support (GET, POST)");
break;
}
}
private function doRequestWithTC3($action, $request, $options, $headers, $payload)
{
$endpoint = $this->getRefreshedEndpoint();
$headers["Host"] = $endpoint;
$headers["X-TC-Action"] = ucfirst($action);
$headers["X-TC-RequestClient"] = $this->sdkVersion;
$headers["X-TC-Timestamp"] = time();
$headers["X-TC-Version"] = $this->apiVersion;
if ($this->region) {
$headers["X-TC-Region"] = $this->region;
}
if ($this->credential->getToken()) {
$headers["X-TC-Token"] = $this->credential->getToken();
}
$language = $this->profile->getLanguage();
if ($language) {
$headers["X-TC-Language"] = $language;
}
$canonicalUri = $this->path;
$reqmethod = $this->profile->getHttpProfile()->getReqMethod();
if (HttpProfile::$REQ_GET == $reqmethod) {
$headers["Content-Type"] = "application/x-www-form-urlencoded";
$rs = $request->serialize();
$am = $this->getPrivateMethod($request, "arrayMerge");
$rsam = $am->invoke($request, $rs);
$canonicalQueryString = http_build_query($rsam);
$payload = "";
}
if (HttpProfile::$REQ_POST == $reqmethod) {
if (isset($options["IsMultipart"]) && $options["IsMultipart"] === true) {
$boundary = uniqid();
$headers["Content-Type"] = "multipart/form-data; boundary=" . $boundary;
$canonicalQueryString = "";
$payload = $this->getMultipartPayload($request, $boundary, $options);
} else if (isset($options["IsOctetStream"]) && $options["IsOctetStream"] === true) {
$headers["Content-Type"] = "application/octet-stream";
$canonicalQueryString = "";
} else if (isset($options["IsCommonJson"]) && $options["IsCommonJson"] === true) {
$headers["Content-Type"] = "application/json";
$canonicalQueryString = "";
$payload = json_encode($request);
} else {
$headers["Content-Type"] = "application/json";
$canonicalQueryString = "";
$payload = $request->toJsonString();
}
}
if ($this->profile->getUnsignedPayload() == true) {
$headers["X-TC-Content-SHA256"] = "UNSIGNED-PAYLOAD";
$payloadHash = hash("SHA256", "UNSIGNED-PAYLOAD");
} else {
$payloadHash = hash("SHA256", $payload);
}
$canonicalHeaders = "content-type:".$headers["Content-Type"]."\n".
"host:".$headers["Host"]."\n";
$signedHeaders = "content-type;host";
$canonicalRequest = $reqmethod."\n".
$canonicalUri."\n".
$canonicalQueryString."\n".
$canonicalHeaders."\n".
$signedHeaders."\n".
$payloadHash;
$algo = "TC3-HMAC-SHA256";
// date_default_timezone_set('UTC');
// $date = date("Y-m-d", $headers["X-TC-Timestamp"]);
$date = gmdate("Y-m-d", $headers["X-TC-Timestamp"]);
$service = explode(".", $endpoint)[0];
$credentialScope = $date."/".$service."/tc3_request";
$hashedCanonicalRequest = hash("SHA256", $canonicalRequest);
$str2sign = $algo."\n".
$headers["X-TC-Timestamp"]."\n".
$credentialScope."\n".
$hashedCanonicalRequest;
$skey = $this->credential->getSecretKey();
$signature = Sign::signTC3($skey, $date, $service, $str2sign);
$sid = $this->credential->getSecretId();
$auth = $algo.
" Credential=".$sid."/".$credentialScope.
", SignedHeaders=content-type;host, Signature=".$signature;
$headers["Authorization"] = $auth;
if (HttpProfile::$REQ_GET == $reqmethod) {
$connect = $this->getConnect();
return $connect->getRequest($this->path, $canonicalQueryString, $headers);
} else {
$connect = $this->getConnect();
return $connect->postRequestRaw($this->path, $headers, $payload);
}
}
private function getMultipartPayload($request, $boundary, $options)
{
$body = "";
$params = $request->serialize();
foreach ($params as $key => $value) {
$body .= "--".$boundary."\r\n";
$body .= "Content-Disposition: form-data; name=\"".$key;
if (in_array($key, $options["BinaryParams"])) {
$body .= "\"; filename=\"".$key;
}
$body .= "\"\r\n";
if (is_array($value)) {
$value = json_encode($value);
$body .= "Content-Type: application/json\r\n";
}
$body .= "\r\n".$value."\r\n";
}
if ($body != "") {
$body .= "--".$boundary."--\r\n";
}
return $body;
}
/**
* @throws TencentCloudSDKException
*/
private function getRequest($action, $request)
{
$query = $this->formatRequestData($action, $request, httpProfile::$REQ_GET);
$connect = $this->getConnect();
return $connect->getRequest($this->path, $query, []);
}
/**
* @throws TencentCloudSDKException
*/
private function postRequest($action, $request)
{
$body = $this->formatRequestData($action, $request, httpProfile::$REQ_POST);
$connect = $this->getConnect();
return $connect->postRequest($this->path, [], $body);
}
/**
* @throws TencentCloudSDKException
*/
private function formatRequestData($action, $request, $reqMethod)
{
$param = $request;
$param["Action"] = ucfirst($action);
$param["RequestClient"] = $this->sdkVersion;
$param["Nonce"] = rand();
$param["Timestamp"] = time();
$param["Version"] = $this->apiVersion;
if ($this->credential->getSecretId()) {
$param["SecretId"] = $this->credential->getSecretId();
}
if ($this->region) {
$param["Region"] = $this->region;
}
if ($this->credential->getToken()) {
$param["Token"] = $this->credential->getToken();
}
if ($this->profile->getSignMethod()) {
$param["SignatureMethod"] = $this->profile->getSignMethod();
}
$language = $this->profile->getLanguage();
if ($language) {
$param["Language"] = $language;
}
$signStr = $this->formatSignString($this->getRefreshedEndpoint(), $this->path, $param, $reqMethod);
$param["Signature"] = Sign::sign($this->credential->getSecretKey(), $signStr, $this->profile->getSignMethod());
return $param;
}
private function createConnect()
{
$prot = $this->profile->getHttpProfile()->getProtocol();
return new HttpConnection($prot.$this->getRefreshedEndpoint(), $this->profile);
}
private function getConnect() {
$keepAlive = $this->profile->getHttpProfile()->getKeepAlive();
if (true === $keepAlive) {
return $this->httpConn;
} else {
return $this->createConnect();
}
}
private function formatSignString($host, $uri, $param, $requestMethod)
{
$tmpParam = [];
ksort($param);
foreach ($param as $key => $value) {
array_push($tmpParam, $key . "=" . $value);
}
$strParam = join ("&", $tmpParam);
$signStr = strtoupper($requestMethod) . $host . $uri ."?".$strParam;
return $signStr;
}
private function getPrivateMethod($obj, $methodName) {
$objReflectClass = new ReflectionClass(get_class($obj));
$method = $objReflectClass->getMethod($methodName);
$method->setAccessible(true);
return $method;
}
/**
* User might call httpProfile.SetEndpoint after client is initialized,
* so everytime we get the enpoint we need to check it.
* Or we must find a way to disable such usage.
*/
private function getRefreshedEndpoint() {
$this->endpoint = $this->profile->getHttpProfile()->getEndpoint();
if ($this->endpoint === null) {
$this->endpoint = $this->service.".".$this->profile->getHttpProfile()->getRootDomain();
}
return $this->endpoint;
}
}

View File

@ -0,0 +1,129 @@
<?php
/*
* Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace TencentCloud\Common;
use \ReflectionClass;
/**
* 抽象model类禁止client引用
* @package TencentCloud\Common
*/
abstract class AbstractModel
{
/**
* 内部实现,用户禁止调用
*/
public function serialize()
{
$ret = $this->objSerialize($this);
return $ret;
}
private function objSerialize($obj) {
$memberRet = [];
$ref = new ReflectionClass(get_class($obj));
$memberList = $ref->getProperties();
foreach ($memberList as $x => $member)
{
$name = ucfirst($member->getName());
$member->setAccessible(true);
$value = $member->getValue($obj);
if ($value === null) {
continue;
}
if ($value instanceof AbstractModel) {
$memberRet[$name] = $this->objSerialize($value);
} else if (is_array($value)) {
$memberRet[$name] = $this->arraySerialize($value);
} else {
$memberRet[$name] = $value;
}
}
return $memberRet;
}
private function arraySerialize($memberList) {
$memberRet = [];
foreach ($memberList as $name => $value)
{
if ($value === null) {
continue;
}
if ($value instanceof AbstractModel) {
$memberRet[$name] = $this->objSerialize($value);
} elseif (is_array($value)) {
$memberRet[$name] = $this->arraySerialize($value);
}else {
$memberRet[$name] = $value;
}
}
return $memberRet;
}
private function arrayMerge($array, $prepend = null)
{
$results = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$results = array_merge($results, static::arrayMerge($value, $prepend.$key.'.'));
}
else {
if (is_bool($value)) {
$results[$prepend.$key] = json_encode($value);
} else {
$results[$prepend.$key] = $value;
}
}
}
return $results;
}
abstract public function deserialize($param);
/**
* @param string $jsonString json格式的字符串
*/
public function fromJsonString($jsonString)
{
$arr = json_decode($jsonString, true);
$this->deserialize($arr);
}
public function toJsonString()
{
$r = $this->serialize();
// it is an object rather than an array
if (empty($r)) {
return "{}";
}
return json_encode($r, JSON_UNESCAPED_UNICODE);
}
public function __call($member, $param)
{
$act = substr($member,0,3);
$attr = substr($member,3);
if ($act === "get") {
return $this->$attr;
} else if ($act === "set") {
$this->$attr = $param[0];
}
}
}

View File

@ -0,0 +1,155 @@
<?php
namespace TencentCloud\Common;
define("STATE_CLOSED", 0);
define("STATE_HALF_OPEN", 1);
define("STATE_OPEN", 2);
class Counter
{
public $failures = 0;
public $total = 0;
public $consecutiveSuccesses = 0;
public $consecutiveFailures = 0;
public function onSuccess()
{
$this->total++;
$this->consecutiveSuccesses++;
$this->consecutiveFailures = 0;
}
public function onFailure()
{
$this->total++;
$this->failures++;
$this->consecutiveFailures++;
$this->consecutiveSuccesses = 0;
}
public function clear()
{
$this->failures = 0;
$this->total = 0;
$this->consecutiveSuccesses = 0;
$this->consecutiveFailures = 0;
}
public function getFailureRate()
{
if ($this->total == 0) {
return 0.0;
}
return floatval($this->failures) / $this->total;
}
}
class CircuitBreaker
{
public $breakerSetting;
public $counter;
public $state;
public $expiry;
public $generation;
public function __construct($breakerSetting)
{
$this->breakerSetting = $breakerSetting;
$this->counter = new Counter();
$this->state = STATE_CLOSED;
$this->expiry = time() + $breakerSetting->windowInterval;
$this->generation = 0;
}
public function readyToOpen()
{
return ($this->counter->failures >= $this->breakerSetting->maxFailNum &&
$this->counter->getFailureRate() >= $this->breakerSetting->maxFailPercent) ||
$this->counter->consecutiveFailures >= 5;
}
public function currentState($now)
{
if ($this->state == STATE_CLOSED) {
if ($this->expiry <= $now) {
$this->toNewGeneration($now);
}
} elseif ($this->state == STATE_OPEN) {
if ($this->expiry <= $now) {
$this->switchState(STATE_HALF_OPEN, $now);
}
}
return array($this->state, $this->generation);
}
public function switchState($newState, $now)
{
if ($this->state == $newState) {
return;
}
$this->state = $newState;
$this->toNewGeneration($now);
}
public function toNewGeneration($now)
{
$this->generation = ($this->generation + 1) % 10;
$this->counter->clear();
if ($this->state == STATE_CLOSED) {
$this->expiry = $now + $this->breakerSetting->windowInterval;
} elseif ($this->state == STATE_OPEN) {
$this->expiry = $now + $this->breakerSetting->timeout;
} else { // STATE_HALF_OPEN
$this->expiry = time();
}
}
public function beforeRequests()
{
$now = time();
list($state, $generation) = $this->currentState($now);
if ($state == STATE_OPEN) {
return array($generation, true);
}
return array($generation, false);
}
public function afterRequests($before, $success)
{
$now = time();
list($state, $generation) = $this->currentState($now);
if ($generation != $before) {
return;
}
if ($success) {
$this->onSuccess($state, $now);
} else {
$this->onFailure($state, $now);
}
}
public function onSuccess($state, $now)
{
if ($state == STATE_CLOSED) {
$this->counter->onSuccess();
} elseif ($state == STATE_HALF_OPEN) {
$this->counter->onSuccess();
if ($this->counter->total - $this->counter->failures >= $this->breakerSetting->maxRequests) {
$this->switchState(STATE_CLOSED, $now);
}
}
}
public function onFailure($state, $now)
{
if ($state == STATE_CLOSED) {
$this->counter->onFailure();
if ($this->readyToOpen()) {
$this->switchState(STATE_OPEN, $now);
}
} elseif ($state == STATE_HALF_OPEN) {
$this->counter->onFailure();
$this->switchState(STATE_OPEN, $now);
}
}
}

View File

@ -0,0 +1,45 @@
<?php
/*
* Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace TencentCloud\Common;
use TencentCloud\Common\AbstractClient;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Credential;
class CommonClient extends AbstractClient
{
protected $endpoint;
protected $service;
protected $version;
/**
* @param string $service
* @param string $version
* @param Credential $credential
* @param string $region
* @param ClientProfile|null $profile
* @throws TencentCloudSDKException
*/
function __construct($service, $version, $credential, $region, $profile=null)
{
$this->service = $service;
$this->version = $version;
$this->endpoint = $service.".tencentcloudapi.com";
parent::__construct($this->endpoint, $version, $credential, $region, $profile);
}
}

View File

@ -0,0 +1,107 @@
<?php
/*
* Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace TencentCloud\Common;
/**
* 证书类,保存认证相关参数
* @package TencentCloud\Common
*/
class Credential
{
/**
* @var string secretId
*/
private $secretId;
/**
* @var string secretKey
*/
private $secretKey;
/**
* @var string token
*/
private $token;
/**
* Credential constructor.
* @param string $secretId secretId
* @param string $secretKey secretKey
* @param string $token token
*/
public function __construct($secretId, $secretKey, $token = null)
{
$this->secretId = $secretId;
$this->secretKey = $secretKey;
$this->token = $token;
}
/**
* 设置secretId
* @param string $secretId secretId
*/
public function setSecretId($secretId)
{
$this->secretId = $secretId;
}
/**
* 设置secretKey
* @param string $secretKey secretKey
*/
public function setSecretKey($secretKey)
{
$this->secretKey = $secretKey;
}
/**
* @param string $token 要设置的token
*/
public function setToken($token)
{
$this->token = $token;
}
/**
* 获取secretId
* @return string secretId
*/
public function getSecretId()
{
return $this->secretId;
}
/**
* 获取secretKey
* @return string secretKey
*/
public function getSecretKey()
{
return $this->secretKey;
}
/**
* 获取token
* @return null|string token
*/
public function getToken()
{
return $this->token;
}
}

View File

@ -0,0 +1,77 @@
<?php
/*
* Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace TencentCloud\Common\Exception;
/**
* sdk异常类
* @package TencentCloud\Common\Exception
*/
class TencentCloudSDKException extends \Exception
{
/**
* @var string 请求id
*/
private $requestId;
private $errorCode;
/**
* TencentCloudSDKException constructor.
* @param string $code 异常错误码
* @param string $message 异常信息
* @param string $requestId 请求ID
*/
public function __construct($code = "", $message = "", $requestId = "")
{
parent::__construct($message, 0);
$this->errorCode = $code;
$this->requestId = $requestId;
}
/**
* 返回请求id
* @return string
*/
public function getRequestId()
{
return $this->requestId;
}
/**
* 返回错误码
* @return string
*/
public function getErrorCode()
{
return $this->errorCode;
}
/**
* 格式化输出异常码异常信息请求id
* @return string
*/
public function __toString()
{
return "[".__CLASS__."]"." code:".$this->errorCode.
" message:".$this->getMessage().
" requestId:".$this->requestId;
}
}

View File

@ -0,0 +1,85 @@
<?php
/*
* Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace TencentCloud\Common\Http;
use GuzzleHttp\Client;
/**
* http连接类
* @package TencentCloud\Common\http
*/
class HttpConnection
{
private $client;
private $profile;
function __construct($url, $profile)
{
$this->client = new Client(["base_uri" => $url]);
$this->profile = $profile;
}
private function getOptions()
{
$options = ["allow_redirects" => false];
$options["timeout"] = $this->profile->getHttpProfile()->getReqTimeout();
$options["proxy"] = $this->profile->getHttpProfile()->getProxy();
return $options;
}
public function getRequest($uri = '', $query = [], $headers = [])
{
$options = $this->getOptions();
if ($query) {
$options["query"] = $query;
}
if ($headers) {
$options["headers"] = $headers;
}
return $this->client->get($uri, $options);
}
public function postRequest($uri = '', $headers = [], $body = '')
{
$options = $this->getOptions();
if ($headers) {
$options["headers"] = $headers;
}
if ($body) {
$options["form_params"] = $body;
}
return $this->client->post($uri, $options);
}
public function postRequestRaw($uri = '', $headers = [], $body = '')
{
$options = $this->getOptions();
if ($headers) {
$options["headers"] = $headers;
}
if ($body) {
$options["body"] = $body;
}
return $this->client->post($uri, $options);
}
}

View File

@ -0,0 +1,227 @@
<?php
/*
* Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace TencentCloud\Common\Profile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
/**
* client可选参数类
* Class ClientProfile
* @package TencentCloud\Common\Profile
*/
class ClientProfile
{
/**
* @var string hmacsha1算法
*/
public static $SIGN_HMAC_SHA1 = "HmacSHA1";
/**
* @var string hmacsha256算法
*/
public static $SIGN_HMAC_SHA256 = "HmacSHA256";
/**
* @var string 签名V3
*/
public static $SIGN_TC3_SHA256 = "TC3-HMAC-SHA256";
/**
* @var string Chinese simplified
*/
public static $ZH_CN = "zh-CN";
/**
* @var string English
*/
public static $EN_US = "en-US";
/**
* @var boolean 开启地域容器
*/
public $enableRegionBreaker;
/**
* @var HttpProfile http相关参数
*/
private $httpProfile;
/**
* @var string 签名方法
*/
private $signMethod;
/**
* @var string 忽略内容签名
*/
private $unsignedPayload;
/**
* @var boolean
*/
private $checkPHPVersion;
/**
* @var string
*/
private $language;
/**
* @var RegionBreakerProfile 地域容灾相关参数
*/
private $regionBreakerProfile;
/**
* ClientProfile constructor.
* @param string $signMethod 签名算法目前支持SHA256SHA1
* @param HttpProfile $httpProfile http参数类
* @param boolean $enableRegionBreaker 开启地域容灾
* @param RegionBreakerProfile $regionBreakerProfile 地域容灾相关参数
*/
public function __construct($signMethod = null, $httpProfile = null, $enableRegionBreaker = false, $regionBreakerProfile = null)
{
$this->signMethod = $signMethod ? $signMethod : ClientProfile::$SIGN_TC3_SHA256;
$this->httpProfile = $httpProfile ? $httpProfile : new HttpProfile();
$this->unsignedPayload = false;
$this->checkPHPVersion = true;
$this->enableRegionBreaker = $enableRegionBreaker;
$this->regionBreakerProfile = $regionBreakerProfile;
//$this->language = ClientProfile::$ZH_CN;
}
/**
* 设置签名算法
* @param string $signMethod 签名方法目前支持SHA256SHA1, TC3
*/
public function setSignMethod($signMethod)
{
$this->signMethod = $signMethod;
}
/**
* 设置http相关参数
* @param HttpProfile $httpProfile http相关参数
*/
public function setHttpProfile($httpProfile)
{
$this->httpProfile = $httpProfile;
}
/**
* 获取签名方法
* @return null|string 签名方法
*/
public function getSignMethod()
{
return $this->signMethod;
}
/**
* 设置是否忽略内容签名
* @param bool $flag true表示忽略签名
*/
public function setUnsignedPayload($flag)
{
$this->unsignedPayload = $flag;
}
/**
* 获取是否忽略内容签名标志位
* @return bool
*/
public function getUnsignedPayload()
{
return $this->unsignedPayload;
}
public function getCheckPHPVersion()
{
return $this->checkPHPVersion;
}
public function setCheckPHPVersion($flag)
{
$this->checkPHPVersion = $flag;
}
public function getLanguage()
{
return $this->language;
}
/**
* @param string $language Valid values: zh-CN, en-US
*/
public function setLanguage($language)
{
$this->language = $language;
}
public function getRegionBreakerProfile()
{
return $this->regionBreakerProfile;
}
/**
* 设置地域容灾相关参数
* @param RegionBreakerProfile $regionBreakerProfile 地域容灾相关参数
*/
public function setRegionBreakerProfile($regionBreakerProfile)
{
$this->regionBreakerProfile = $regionBreakerProfile;
}
/**
* 获取http选项实例
* @return null|HttpProfile http选项实例
*/
public function getHttpProfile()
{
return $this->httpProfile;
}
}
class RegionBreakerProfile {
public function __construct($masterEndpoint,
$slaveEndpoint,
$maxFailNum = 5,
$maxFailPercent = 0.75,
$windowInterval = 60*5,
$timeout = 60,
$maxRequests = 5) {
if (empty($masterEndpoint) || !(substr($masterEndpoint, -20) === '.tencentcloudapi.com')) {
throw new TencentCloudSDKException("", 'masterEndpoint must be provided and end with ".tencentcloudapi.com"');
}
if (empty($slaveEndpoint) || !(substr($slaveEndpoint, -20) === '.tencentcloudapi.com')) {
throw new TencentCloudSDKException("", 'slaveEndpoint must be provided and end with ".tencentcloudapi.com"');
}
$this->masterEndpoint = $masterEndpoint;
$this->slaveEndpoint = $slaveEndpoint;
$this->maxFailNum = $maxFailNum;
$this->maxFailPercent = $maxFailPercent;
if ($this->maxFailPercent < 0 || $this->maxFailPercent > 1) {
throw new TencentCloudSDKException("", "ClientError: max fail percent must be set between 0 and 1");
}
$this->windowInterval = $windowInterval;
$this->timeout = $timeout;
$this->maxRequests = $maxRequests;
}
}

View File

@ -0,0 +1,215 @@
<?php
/*
* Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace TencentCloud\Common\Profile;
/**
* http相关参数类
* Class HttpProfile
* @package TencentCloud\Common\Profile
*/
class HttpProfile
{
/**
* @var string https访问
*/
public static $REQ_HTTPS = "https://";
/**
* @var string http访问
*/
public static $REQ_HTTP = "http://";
/**
* @var string post请求
*/
public static $REQ_POST = "POST";
/**
* @var string get请求
*/
public static $REQ_GET = "GET";
/**
* @var int 时间一分钟
*/
public static $TM_MINUTE = 60;
/**
* @var string http请求方法
*/
private $reqMethod;
/**
* @var string 请求接入点域名
*/
private $endpoint;
/**
* @var integer 请求超时时长,单位为秒
*/
private $reqTimeout;
/**
* @var string 请求协议
*/
private $protocol;
/**
* @var string|array 请求代理
*/
private $proxy;
/**
* @var string
*/
private $rootDomain;
/**
* @var boolean
*/
private $keepAlive;
/**
* HttpProfile constructor.
* @param string $protocol 请求协议
* @param string $endpoint 请求接入点域名(xx.[region.]tencentcloudapi.com)
* @param string $reqMethod http请求方法目前支持POST GET
* @param integer $reqTimeout 请求超时时间,单位:s
*/
public function __construct($protocol = null, $endpoint = null, $reqMethod = null, $reqTimeout = null)
{
$this->reqMethod = $reqMethod ? $reqMethod : HttpProfile::$REQ_POST;
$this->endpoint = $endpoint;
$this->reqTimeout = $reqTimeout ? $reqTimeout : HttpProfile::$TM_MINUTE;
$this->protocol = $protocol ? $protocol : HttpProfile::$REQ_HTTPS;
$this->rootDomain = "tencentcloudapi.com";
$this->keepAlive = false;
}
/**
* 设置http请求方法
* @param string $reqMethod http请求方法目前支持POST GET
*/
public function setReqMethod($reqMethod)
{
$this->reqMethod = $reqMethod;
}
/**
* 设置请求协议
* @param string $protocol 请求协议https:// http://
*/
public function setProtocol($protocol) {
$this->protocol = $protocol;
}
/**
* 设置请求接入点域名
* @param string $endpoint 请求接入点域名(xx.[region.]tencentcloudapi.com)
*/
public function setEndpoint($endpoint)
{
$this->endpoint = $endpoint;
}
/**
* 设置请求超时时间
* @param integer $reqTimeout 请求超时时间,单位:s
*/
public function setReqTimeout($reqTimeout)
{
$this->reqTimeout = $reqTimeout;
}
/**
* 设置请求代理
* @param string|array $proxy 请求代理配置
*/
public function setProxy($proxy)
{
$this->proxy = $proxy;
}
/**
* 获取请求方法
* @return null|string 请求方法
*/
public function getReqMethod()
{
return $this->reqMethod;
}
/**
* 获取请求协议
* @return null|string 请求协议
*/
public function getProtocol()
{
return $this->protocol;
}
/**
* 获取请求超时时间
* @return int 请求超时时间
*/
public function getReqTimeout()
{
return $this->reqTimeout;
}
/**
* 获取请求接入点域名
* @return null|string 接入点域名
*/
public function getEndpoint()
{
return $this->endpoint;
}
/**
* 获取请求代理
* @return null|string|array
*/
public function getProxy()
{
return $this->proxy;
}
public function setRootDomain($domain)
{
$this->rootDomain = $domain;
}
public function getRootDomain()
{
return $this->rootDomain;
}
/**
* @param boolean $flag
*/
public function setKeepAlive($flag) {
$this->keepAlive = $flag;
}
public function getKeepAlive() {
return $this->keepAlive;
}
}

View File

@ -0,0 +1,49 @@
<?php
/*
* Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace TencentCloud\Common;
use TencentCloud\Common\Exception\TencentCloudSDKException;
/**
* 签名类禁止client引用
* @package TencentCloud\Common
* @throws TencentCloudSDKException
*/
class Sign
{
/**
* @throws TencentCloudSDKException
*/
public static function sign($secretKey, $signStr, $signMethod)
{
$signMethodMap = ["HmacSHA1" => "SHA1", "HmacSHA256" => "SHA256"];
if (!array_key_exists($signMethod, $signMethodMap)) {
throw new TencentCloudSDKException("signMethod invalid", "signMethod only support (HmacSHA1, HmacSHA256)");
}
$signature = base64_encode(hash_hmac($signMethodMap[$signMethod], $signStr, $secretKey, true));
return $signature;
}
public static function signTC3($skey, $date, $service, $str2sign)
{
$dateKey = hash_hmac("SHA256", $date, "TC3".$skey, true);
$serviceKey = hash_hmac("SHA256", $service, $dateKey, true);
$reqKey = hash_hmac("SHA256", "tc3_request", $serviceKey, true);
return hash_hmac("SHA256", $str2sign, $reqKey);
}
}