├── CHANGELOG.rst ├── README.md ├── composer.json ├── demo.php ├── dynamic-module-demo.php ├── examples ├── apigateway │ └── GetAccountSettings.php ├── athena │ └── WelcomeMessage.php ├── batch │ └── DescribeComputeEnvs.php ├── ccs │ └── DescribeCluster.php ├── cvm │ ├── DescribeInstances.php │ └── v20170312 │ │ ├── DescribeInstances.php │ │ └── RebootInstances.php ├── dfw │ └── DescribeSecurityGroups.php ├── live │ └── CreateLVBChannel.php ├── partners │ └── QueryClientDeals.php ├── redis │ └── DescribeRedisRegions.php ├── scf │ └── ListFunctions.php ├── sts │ └── GetFederationToken.php └── tmt │ └── LanguageDetect.php └── src └── QcloudApi ├── Common ├── Base.php ├── Error.php ├── Request.php └── Sign.php ├── Module ├── Account.php ├── Apigateway.php ├── Athena.php ├── Base.php ├── Batch.php ├── Bgpip.php ├── Bill.php ├── Bm.php ├── Bmeip.php ├── Bmlb.php ├── Bmvpc.php ├── Cbs.php ├── Ccr.php ├── Ccs.php ├── Cdb.php ├── Cdn.php ├── Cloudaudit.php ├── Cmem.php ├── Cns.php ├── Cvm.php ├── Dfw.php ├── Eip.php ├── Emr.php ├── Feecenter.php ├── Image.php ├── Lb.php ├── Live.php ├── Market.php ├── Monitor.php ├── Morphling.php ├── Partners.php ├── Redis.php ├── Scaling.php ├── Scf.php ├── Sec.php ├── Snapshot.php ├── Sts.php ├── Tbaas.php ├── Tdsql.php ├── Tmt.php ├── Trade.php ├── Vod.php ├── Vpc.php ├── Wenzhi.php └── Yunsou.php └── QcloudApi.php /CHANGELOG.rst: -------------------------------------------------------------------------------- 1 | ========== 2 | CHANGE LOG 3 | ========== 4 | 5 | latest (now) 6 | ============ 7 | 8 | 2.0.9 (2018-08-03) 9 | ================== 10 | 11 | * fix undefined warning 12 | 13 | 2.0.8 (2018-07-18) 14 | ================== 15 | 16 | * support dynamic module: Now user can directly use any product's API, no longer require adding its source code file in src/QcloudApi/Module. 17 | 18 | 2.0.7 (2018-04-16) 19 | ================== 20 | 21 | * add ccr module 22 | 23 | 2.0.6 (2018-03-02) 24 | ================== 25 | 26 | * add emr module 27 | * add tbaas module 28 | * add athena module 29 | 30 | 2.0.5 (2018-02-06) 31 | ================== 32 | 33 | * add sts module 34 | * add partners module 35 | 36 | 2.0.4 (2017-12-28) 37 | ================== 38 | 39 | * add redis module 40 | * add batch module 41 | * add ccs module 42 | 43 | 2.0.3 (2017-12-27) 44 | ================== 45 | 46 | * add tmt module 47 | * add apigateway module 48 | * add dfw module 49 | * add scf module 50 | 51 | history 52 | ======= 53 | 54 | * [2017/11/16] 新增Cloudaudit模块 55 | * [2017/10/30] 新增Bgpip模块 56 | * [2017/9/11] 新增Bmeip和Bmvpc模块 57 | * [2017/8/7] 新增Feecenter模块 58 | * [2017/7/31] 新增Bmlb模块 59 | * [2017/7/12] 回滚:不默认传Version参数 60 | * [5/19]设置接口默认Version: Cvm模块新版本API已经上线,通过是否传Version区分新旧版本。SDK默认调用新接口,因此需要增加Version的默认设置。 CvmAPI接口介绍见:https://www.qcloud.com/document/api/213/569 61 | * [3/1]增加对HmacSHA1签名和HmacSHA256签名兼容的支持 62 | * [7/15]增加Tdsql模块 63 | * [7/6] 增加Cmem模块 64 | * [6/17] 增加account模块 65 | * [5/25] 添加Cbs、Snapshot和Scaling模块 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # qcloudapi-sdk-php 2 | 3 | qcloudapi-sdk-php是为了让PHP开发者能够在自己的代码里更快捷方便的使用腾讯云的API而开发的SDK工具包。 4 | 5 | ## 资源 6 | 7 | * [公共参数](http://wiki.qcloud.com/wiki/%E5%85%AC%E5%85%B1%E5%8F%82%E6%95%B0) 8 | * [API列表](http://wiki.qcloud.com/wiki/API) 9 | * [错误码](http://wiki.qcloud.com/wiki/%E9%94%99%E8%AF%AF%E7%A0%81) 10 | 11 | ## 入门 12 | 13 | 1. 申请安全凭证。 14 | 在第一次使用云API之前,用户首先需要在腾讯云网站上申请安全凭证,安全凭证包括 SecretId 和 SecretKey, SecretId 是用于标识 API 调用者的身份,SecretKey是用于加密签名字符串和服务器端验证签名字符串的密钥。SecretKey 必须严格保管,避免泄露。 15 | 16 | 2. 下载SDK,放入到您的程序目录。 17 | 使用方法请参考下面的例子。 18 | 19 | ## 例子 20 | 21 | '你的secretId', 25 | 'SecretKey' => '你的secretKey', 26 | 'RequestMethod' => 'GET', 27 | 'DefaultRegion' => 'gz'); 28 | 29 | // 第一个参数表示使用哪个域名,例如: 30 | // QcloudApi::MODULE_CVM 对应 cvm.api.qcloud.com 31 | // 对于没有被显式定义的模块,你也可以使用动态模块,例如: 32 | // $service = QcloudApi::load("ckafka", $config); 33 | $service = QcloudApi::load(QcloudApi::MODULE_CVM, $config); 34 | 35 | // 请求参数,请参考wiki文档上对应接口的说明 36 | $package = array('offset' => 0, 37 | 'limit' => 3, 38 | // 'Region' => 'sh', // 当Region不是上面配置的DefaultRegion值时,可以重新指定请求的Region 39 | 'SignatureMethod' => 'HmacSHA256',//指定所要用的签名算法,可选HmacSHA256或HmacSHA1,默认为HmacSHA1 40 | ); 41 | 42 | // 请求前可以通过下面四个方法重新设置请求的secretId/secretKey/region/method参数 43 | // 重新设置secretId 44 | $secretId = '你的secretId'; 45 | $service->setConfigSecretId($secretId); 46 | // 重新设置secretKey 47 | $secretKey = '你的secretKey'; 48 | $service->setConfigSecretKey($secretKey); 49 | // 重新设置region 50 | $region = 'sh'; 51 | $service->setConfigDefaultRegion($region); 52 | // 重新设置method 53 | $method = 'POST'; 54 | $service->setConfigRequestMethod($method); 55 | 56 | // 请求方法为对应接口的接口名,请参考wiki文档上对应接口的接口名 57 | $a = $service->DescribeInstances($package); 58 | 59 | // 生成请求的URL,不发起请求 60 | $a = $service->generateUrl('DescribeInstances', $package); 61 | 62 | if ($a === false) { 63 | // 请求失败,解析错误信息 64 | $error = $service->getError(); 65 | echo "Error code:" . $error->getCode() . ' message:' . $error->getMessage(); 66 | 67 | // 对于异步任务接口,可以通过下面的方法获取对应任务执行的信息 68 | $detail = $error->getExt(); 69 | } else { 70 | // 请求成功 71 | var_dump($a); 72 | } 73 | 74 | ## 动态模块 75 | 76 | 以前SDK需要把每一个产品都建一个类文件,指定域名,这虽然加强了合法性检查,但是新产品上线后,可能没有同步到SDK,造成开发者使用不便。2.0.8及更高的版本支持动态模块,你可以直接按模块名初始化一个service对象,例如:``$service = QcloudApi::load("ckafka", $config);``,这里ckafka并未有对应的类文件,但是仍然可以初始化成功并执行接下来的接口调用。注意,代码中实际上是把模块名拼接API的根域名进行调用的,而极个别的产品其模块名和域名不对应,典型的如cmq的域名首部是带地域信息的,此时模块名应该遵从域名首部。 77 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tencentyun-api/qcloudapi-sdk-php", 3 | "keywords": ["tencentyun", "qcloud", "sdk"], 4 | "homepage": "http://www.qcloud.com/", 5 | "license": "MIT", 6 | "description": "php sdk for qcloudapi", 7 | "require": { 8 | "php": ">=5.3.0" 9 | }, 10 | "autoload": { 11 | "classmap": [ 12 | "src/QcloudApi/QcloudApi.php" 13 | ] 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /demo.php: -------------------------------------------------------------------------------- 1 | getenv("QCLOUD_SECRET_ID"), //'你的secretId',需要在环境变量中设置 5 | 'SecretKey' => getenv("QCLOUD_SECRET_KEY"), //'你的secretKey',需要在环境变量中设置 6 | 'RequestMethod' => 'GET', 7 | 'DefaultRegion' => 'gz'); 8 | 9 | $cvm = QcloudApi::load(QcloudApi::MODULE_CVM, $config); 10 | 11 | $package = array('offset' => 0, 'limit' => 3, 'SignatureMethod' =>'HmacSHA256'); 12 | 13 | $a = $cvm->DescribeInstances($package); 14 | // $a = $cvm->generateUrl('DescribeInstances', $package); 15 | 16 | if ($a === false) { 17 | $error = $cvm->getError(); 18 | echo "Error code:" . $error->getCode() . ".\n"; 19 | echo "message:" . $error->getMessage() . ".\n"; 20 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 21 | } 22 | 23 | echo "Request: " . $cvm->getLastRequest(); 24 | echo "\nResponse: " . $cvm->getLastResponse(); 25 | echo "\n"; 26 | -------------------------------------------------------------------------------- /dynamic-module-demo.php: -------------------------------------------------------------------------------- 1 | getenv("QCLOUD_SECRET_ID"), //'你的secretId',需要在环境变量中设置 5 | 'SecretKey' => getenv("QCLOUD_SECRET_KEY"), //'你的secretKey',需要在环境变量中设置 6 | 'RequestMethod' => 'GET', 7 | 'DefaultRegion' => 'gz'); 8 | 9 | // ckafa is not explicitly supported in src/QcloudApi/Module 10 | $ckafka = QcloudApi::load("ckafka", $config); 11 | 12 | $a = $ckafka->ListInstance(); 13 | 14 | if ($a === false) { 15 | $error = $ckafka->getError(); 16 | echo "Error code:" . $error->getCode() . ".\n"; 17 | echo "message:" . $error->getMessage() . ".\n"; 18 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 19 | } 20 | 21 | echo "Request: " . $ckafka->getLastRequest(); 22 | echo "\nResponse: " . $ckafka->getLastResponse(); 23 | echo "\n"; 24 | -------------------------------------------------------------------------------- /examples/apigateway/GetAccountSettings.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'gz'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_APIGATEWAY, $config); 12 | 13 | $package = array('SignatureMethod' =>'HmacSHA256'); 14 | 15 | $rsp = $api->GetAccountSettings($package); 16 | 17 | if ($rsp === false) { 18 | $error = $api->getError(); 19 | echo "Error code:" . $error->getCode() . ".\n"; 20 | echo "message:" . $error->getMessage() . ".\n"; 21 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 22 | } else { 23 | var_dump($rsp); 24 | } 25 | 26 | echo "\nRequest :" . $api->getLastRequest(); 27 | echo "\nResponse :" . $api->getLastResponse(); 28 | echo "\n"; 29 | -------------------------------------------------------------------------------- /examples/athena/WelcomeMessage.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'gz'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_ATHENA, $config); 12 | 13 | $package = array( 14 | 'SignatureMethod' =>'HmacSHA256', 15 | 'InstanceId' => '4d8573a2-ff42-11e7-8858-525400bb7b8b', 16 | 'AccessChannelCode' => 'default', 17 | ); 18 | 19 | $rsp = $api->WelcomeMessage($package); 20 | 21 | if ($rsp === false) { 22 | $error = $api->getError(); 23 | echo "Error code:" . $error->getCode() . ".\n"; 24 | echo "message:" . $error->getMessage() . ".\n"; 25 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 26 | } else { 27 | var_dump($rsp); 28 | } 29 | 30 | echo "\nRequest :" . $api->getLastRequest(); 31 | echo "\nResponse :" . $api->getLastResponse(); 32 | echo "\n"; 33 | -------------------------------------------------------------------------------- /examples/batch/DescribeComputeEnvs.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'gz'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_BATCH, $config); 12 | 13 | $package = array('SignatureMethod' =>'HmacSHA256', 'Version' => '2017-03-12'); 14 | 15 | $rsp = $api->DescribeComputeEnvs($package); 16 | 17 | if ($rsp === false) { 18 | $error = $api->getError(); 19 | echo "Error code:" . $error->getCode() . ".\n"; 20 | echo "message:" . $error->getMessage() . ".\n"; 21 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 22 | } else { 23 | var_dump($rsp); 24 | } 25 | 26 | echo "\nRequest :" . $api->getLastRequest(); 27 | echo "\nResponse :" . $api->getLastResponse(); 28 | echo "\n"; 29 | -------------------------------------------------------------------------------- /examples/ccs/DescribeCluster.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'gz'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_CCS, $config); 12 | 13 | $package = array('SignatureMethod' =>'HmacSHA256'); 14 | 15 | $rsp = $api->DescribeCluster($package); 16 | 17 | if ($rsp === false) { 18 | $error = $api->getError(); 19 | echo "Error code:" . $error->getCode() . ".\n"; 20 | echo "message:" . $error->getMessage() . ".\n"; 21 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 22 | } else { 23 | var_dump($rsp); 24 | } 25 | 26 | echo "\nRequest :" . $api->getLastRequest(); 27 | echo "\nResponse :" . $api->getLastResponse(); 28 | echo "\n"; 29 | -------------------------------------------------------------------------------- /examples/cvm/DescribeInstances.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'POST', 9 | 'DefaultRegion' => 'ap-guangzhou'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_CVM, $config); 12 | 13 | $package = array( 14 | 'SignatureMethod' =>'HmacSHA256', 15 | ); 16 | 17 | $rsp = $api->DescribeInstances($package); 18 | 19 | if ($rsp === false) { 20 | $error = $api->getError(); 21 | echo "Error code:" . $error->getCode() . ".\n"; 22 | echo "message:" . $error->getMessage() . ".\n"; 23 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 24 | } else { 25 | var_dump($rsp); 26 | } 27 | 28 | echo "\nRequest :" . $api->getLastRequest(); 29 | echo "\nResponse :" . $api->getLastResponse(); 30 | echo "\n"; 31 | -------------------------------------------------------------------------------- /examples/cvm/v20170312/DescribeInstances.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'POST', 9 | 'DefaultRegion' => 'ap-guangzhou'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_CVM, $config); 12 | 13 | $package = array( 14 | 'SignatureMethod' =>'HmacSHA256', 15 | 'Version' => '2017-03-12'); 16 | 17 | $rsp = $api->DescribeInstances($package); 18 | 19 | if ($rsp === false) { 20 | $error = $api->getError(); 21 | echo "Error code:" . $error->getCode() . ".\n"; 22 | echo "message:" . $error->getMessage() . ".\n"; 23 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 24 | } else { 25 | var_dump($rsp); 26 | } 27 | -------------------------------------------------------------------------------- /examples/cvm/v20170312/RebootInstances.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'POST', 9 | 'DefaultRegion' => 'ap-guangzhou'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_CVM, $config); 12 | 13 | $package = array( 14 | 'SignatureMethod' =>'HmacSHA256', 15 | 'Version' => '2017-03-12', 16 | 'InstanceIds.0' => 'ins-ehyxzkge' 17 | ); 18 | 19 | $rsp = $api->RebootInstances($package); 20 | 21 | if ($rsp === false) { 22 | $error = $api->getError(); 23 | echo "Error code:" . $error->getCode() . ".\n"; 24 | echo "message:" . $error->getMessage() . ".\n"; 25 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 26 | } else { 27 | var_dump($rsp); 28 | } 29 | -------------------------------------------------------------------------------- /examples/dfw/DescribeSecurityGroups.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'gz'); 10 | 11 | $dfw = QcloudApi::load(QcloudApi::MODULE_DFW, $config); 12 | 13 | $package = array('SignatureMethod' =>'HmacSHA256'); 14 | 15 | $a = $dfw->DescribeSecurityGroups($package); 16 | 17 | if ($a === false) { 18 | $error = $dfw->getError(); 19 | echo "Error code:" . $error->getCode() . ".\n"; 20 | echo "message:" . $error->getMessage() . ".\n"; 21 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 22 | } else { 23 | var_dump($a); 24 | } 25 | 26 | echo "\nRequest :" . $dfw->getLastRequest(); 27 | echo "\nResponse :" . $dfw->getLastResponse(); 28 | echo "\n"; 29 | -------------------------------------------------------------------------------- /examples/live/CreateLVBChannel.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'ap-guangzhou'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_LIVE, $config); 12 | 13 | $package = array( 14 | 'SignatureMethod' => 'HmacSHA256', 15 | 'channelName' => 'test', 16 | 'outputSourceType' => 1, 17 | 'sourceList.0.name' => 'test', 18 | 'sourceList.0.type' => 1, 19 | ); 20 | 21 | $rsp = $api->CreateLVBChannel($package); 22 | 23 | if ($rsp === false) { 24 | $error = $api->getError(); 25 | echo "Error code:" . $error->getCode() . ".\n"; 26 | echo "message:" . $error->getMessage() . ".\n"; 27 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 28 | } else { 29 | var_dump($rsp); 30 | } 31 | 32 | echo "\nRequest :" . $api->getLastRequest(); 33 | echo "\nResponse :" . $api->getLastResponse(); 34 | echo "\n"; 35 | -------------------------------------------------------------------------------- /examples/partners/QueryClientDeals.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'gz'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_PARTNERS, $config); 12 | 13 | $package = array('SignatureMethod' =>'HmacSHA256'); 14 | 15 | $rsp = $api->QueryClientDeals($package); 16 | 17 | if ($rsp === false) { 18 | $error = $api->getError(); 19 | echo "Error code:" . $error->getCode() . ".\n"; 20 | echo "message:" . $error->getMessage() . ".\n"; 21 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 22 | } else { 23 | var_dump($rsp); 24 | } 25 | 26 | echo "\nRequest :" . $api->getLastRequest(); 27 | echo "\nResponse :" . $api->getLastResponse(); 28 | echo "\n"; 29 | -------------------------------------------------------------------------------- /examples/redis/DescribeRedisRegions.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'gz'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_REDIS, $config); 12 | 13 | $package = array('SignatureMethod' =>'HmacSHA256'); 14 | 15 | $rsp = $api->DescribeRedisRegions($package); 16 | 17 | if ($rsp === false) { 18 | $error = $api->getError(); 19 | echo "Error code:" . $error->getCode() . ".\n"; 20 | echo "message:" . $error->getMessage() . ".\n"; 21 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 22 | } else { 23 | var_dump($rsp); 24 | } 25 | 26 | echo "\nRequest :" . $api->getLastRequest(); 27 | echo "\nResponse :" . $api->getLastResponse(); 28 | echo "\n"; 29 | -------------------------------------------------------------------------------- /examples/scf/ListFunctions.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'gz'); 10 | 11 | $scf = QcloudApi::load(QcloudApi::MODULE_SCF, $config); 12 | 13 | $package = array('SignatureMethod' =>'HmacSHA256'); 14 | 15 | $a = $scf->ListFunctions($package); 16 | 17 | if ($a === false) { 18 | $error = $scf->getError(); 19 | echo "Error code:" . $error->getCode() . ".\n"; 20 | echo "message:" . $error->getMessage() . ".\n"; 21 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 22 | } else { 23 | var_dump($a); 24 | } 25 | 26 | echo "\nRequest :" . $scf->getLastRequest(); 27 | echo "\nResponse :" . $scf->getLastResponse(); 28 | echo "\n"; 29 | -------------------------------------------------------------------------------- /examples/sts/GetFederationToken.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'gz'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_STS, $config); 12 | 13 | $package = array( 14 | 'SignatureMethod' =>'HmacSHA256', 15 | 'policy' => '{"statement":[{"action":["name/cos:GetObject","name/cos:PutObject"],"effect":"allow","resource":["qcs::cos:cn-north:uin/3411212962:prefix//3411212962/sevenyou/*"]}],"version":"2.0"}', 16 | 'name' => 'test', 17 | ); 18 | 19 | $rsp = $api->GetFederationToken($package); 20 | 21 | if ($rsp === false) { 22 | $error = $api->getError(); 23 | echo "Error code:" . $error->getCode() . ".\n"; 24 | echo "message:" . $error->getMessage() . ".\n"; 25 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 26 | } else { 27 | var_dump($rsp); 28 | } 29 | 30 | echo "\nRequest :" . $api->getLastRequest(); 31 | echo "\nResponse :" . $api->getLastResponse(); 32 | echo "\n"; 33 | -------------------------------------------------------------------------------- /examples/tmt/LanguageDetect.php: -------------------------------------------------------------------------------- 1 | getenv('TENCENTCLOUD_SECRET_ID'), 7 | 'SecretKey' => getenv('TENCENTCLOUD_SECRET_KEY'), 8 | 'RequestMethod' => 'GET', 9 | 'DefaultRegion' => 'gz'); 10 | 11 | $api = QcloudApi::load(QcloudApi::MODULE_TMT, $config); 12 | 13 | $package = array( 14 | 'SignatureMethod' =>'HmacSHA256', 15 | 'text' => 'Hello world!'); 16 | 17 | $rsp = $api->LanguageDetect($package); 18 | 19 | if ($rsp === false) { 20 | $error = $api->getError(); 21 | echo "Error code:" . $error->getCode() . ".\n"; 22 | echo "message:" . $error->getMessage() . ".\n"; 23 | echo "ext:" . var_export($error->getExt(), true) . ".\n"; 24 | } else { 25 | var_dump($rsp); 26 | } 27 | 28 | echo "\nRequest :" . $api->getLastRequest(); 29 | echo "\nResponse :" . $api->getLastResponse(); 30 | echo "\n"; 31 | -------------------------------------------------------------------------------- /src/QcloudApi/Common/Base.php: -------------------------------------------------------------------------------- 1 | _error = new QcloudApi_Common_Error($code, $message, $ext); 27 | return $this->_error; 28 | } 29 | 30 | /** 31 | * getError 32 | * 获取错误信息 33 | * 34 | * @return object 35 | */ 36 | public function getError() 37 | { 38 | return $this->_error; 39 | } 40 | } -------------------------------------------------------------------------------- /src/QcloudApi/Common/Error.php: -------------------------------------------------------------------------------- 1 | _code = $code ? $code : self::LOCAL_ERROR_CODE; 45 | $this->_message = $message; 46 | $this->_ext = $ext; 47 | } 48 | 49 | /** 50 | * getCode 51 | * 获取错误号 52 | */ 53 | public function getCode() 54 | { 55 | return $this->_code; 56 | } 57 | 58 | /** 59 | * getMessage 60 | * 获取错误信息 61 | */ 62 | public function getMessage() 63 | { 64 | return $this->_message; 65 | } 66 | 67 | /** 68 | * getExt 69 | * 获取扩展信息 70 | */ 71 | public function getExt() 72 | { 73 | return $this->_ext; 74 | } 75 | } -------------------------------------------------------------------------------- /src/QcloudApi/Common/Request.php: -------------------------------------------------------------------------------- 1 | $value) 71 | { 72 | if ($key == 'Signature') 73 | { 74 | continue; 75 | } 76 | // 排除上传文件的参数 77 | if ($requestMethod == 'POST' && substr($value, 0, 1) == '@') { 78 | continue; 79 | } 80 | // 把 参数中的 _ 替换成 . 81 | if (strpos($key, '_')) 82 | { 83 | $key = str_replace('_', '.', $key); 84 | } 85 | 86 | if ($i == 0) 87 | { 88 | $paramStr .= '?'; 89 | } 90 | else 91 | { 92 | $paramStr .= '&'; 93 | } 94 | $paramStr .= $key . '=' . $value; 95 | ++$i; 96 | } 97 | 98 | return $paramStr; 99 | } 100 | 101 | } 102 | 103 | -------------------------------------------------------------------------------- /src/QcloudApi/Module/Account.php: -------------------------------------------------------------------------------- 1 | setConfig($config); 63 | } 64 | 65 | /** 66 | * setConfig 67 | * 设置配置 68 | * @param array $config 模块配置 69 | */ 70 | public function setConfig($config) 71 | { 72 | if (!is_array($config) || !count($config)) 73 | return false; 74 | 75 | foreach ($config as $key => $val) { 76 | switch ($key) { 77 | case 'SecretId': 78 | $this->setConfigSecretId($val); 79 | break; 80 | 81 | case 'SecretKey': 82 | $this->setConfigSecretKey($val); 83 | break; 84 | 85 | case 'DefaultRegion': 86 | $this->setConfigDefaultRegion($val); 87 | break; 88 | 89 | case 'RequestMethod': 90 | $this->setConfigRequestMethod($val); 91 | break; 92 | 93 | default: 94 | ; 95 | break; 96 | } 97 | } 98 | 99 | return true; 100 | } 101 | 102 | /** 103 | * setConfigSecretId 104 | * 设置secretId 105 | * @param string $secretId secretId 106 | */ 107 | public function setConfigSecretId($secretId) 108 | { 109 | $this->_secretId = $secretId; 110 | return $this; 111 | } 112 | 113 | /** 114 | * setConfigSecretKey 115 | * 设置secretKey 116 | * @param string $secretKey 117 | */ 118 | public function setConfigSecretKey($secretKey) 119 | { 120 | $this->_secretKey = $secretKey; 121 | return $this; 122 | } 123 | 124 | /** 125 | * setConfigDefaultRegion 126 | * 设置区域参数 127 | * @param string $region 128 | */ 129 | public function setConfigDefaultRegion($region) 130 | { 131 | $this->_defaultRegion = $region; 132 | return $this; 133 | } 134 | 135 | /** 136 | * setConfigRequestMethod 137 | * 设置请求方法 138 | * @param string $method 139 | */ 140 | public function setConfigRequestMethod($method) 141 | { 142 | $this->_requestMethod = strtoupper($method); 143 | return $this; 144 | } 145 | 146 | /** 147 | * getLastRequest 148 | * 获取上次请求的url 149 | * @return 150 | */ 151 | public function getLastRequest() 152 | { 153 | require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php'; 154 | return QcloudApi_Common_Request::getRequestUrl(); 155 | } 156 | 157 | /** 158 | * getLastResponse 159 | * 获取请求的原始返回 160 | * @return 161 | */ 162 | public function getLastResponse() 163 | { 164 | require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php'; 165 | return QcloudApi_Common_Request::getRawResponse(); 166 | } 167 | 168 | /** 169 | * generateUrl 170 | * 生成请求的URL,不发起请求 171 | * @param string $name 接口方法名 172 | * @param array $params 请求参数 173 | * @return 174 | */ 175 | public function generateUrl($name, $params) 176 | { 177 | require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php'; 178 | 179 | $action = ucfirst($name); 180 | $params['Action'] = $action; 181 | 182 | if (!isset($params['Region'])) 183 | $params['Region'] = $this->_defaultRegion; 184 | 185 | return QcloudApi_Common_Request::generateUrl($params, $this->_secretId, $this->_secretKey, $this->_requestMethod, 186 | $this->_serverHost, $this->_serverUri); 187 | } 188 | 189 | /** 190 | * __call 191 | * 通过__call转发请求 192 | * @param string $name 方法名 193 | * @param array $arguments 参数 194 | * @return 195 | */ 196 | public function __call($name, $arguments) 197 | { 198 | $response = $this->_dispatchRequest($name, $arguments); 199 | 200 | return $this->_dealResponse($response); 201 | } 202 | 203 | /** 204 | * _dispatchRequest 205 | * 发起接口请求 206 | * @param string $name 接口名 207 | * @param array $arguments 接口参数 208 | * @return 209 | */ 210 | protected function _dispatchRequest($name, $arguments) 211 | { 212 | $action = ucfirst($name); 213 | 214 | $params = array(); 215 | if (is_array($arguments) && !empty($arguments)) { 216 | $params = (array) $arguments[0]; 217 | } 218 | $params['Action'] = $action; 219 | 220 | if (!isset($params['Region'])) 221 | $params['Region'] = $this->_defaultRegion; 222 | 223 | require_once QCLOUDAPI_ROOT_PATH . '/Common/Request.php'; 224 | 225 | $response = QcloudApi_Common_Request::send($params, $this->_secretId, $this->_secretKey, $this->_requestMethod, 226 | $this->_serverHost, $this->_serverUri); 227 | 228 | return $response; 229 | } 230 | 231 | /** 232 | * _dealResponse 233 | * 处理返回 234 | * @param array $rawResponse 235 | * @return 236 | */ 237 | protected function _dealResponse($rawResponse) 238 | { 239 | if (!is_array($rawResponse) || (!isset($rawResponse['code']) && !isset($rawResponse['Response']))) { 240 | $this->setError("", 'request falied!'); 241 | return false; 242 | } 243 | 244 | if (isset($rawResponse['code']) && $rawResponse['code']) { 245 | $ext = ''; 246 | require_once QCLOUDAPI_ROOT_PATH . '/Common/Error.php'; 247 | if (isset($rawResponse['detail'])) { 248 | // 批量异步操作,返回任务失败信息 249 | $ext = $rawResponse['detail']; 250 | } 251 | $this->setError($rawResponse['code'], $rawResponse['message'], $ext); 252 | return false; 253 | } 254 | 255 | unset($rawResponse['code'], $rawResponse['message']); 256 | 257 | if (count($rawResponse)) 258 | return $rawResponse; 259 | else 260 | return true; 261 | } 262 | } 263 | -------------------------------------------------------------------------------- /src/QcloudApi/Module/Batch.php: -------------------------------------------------------------------------------- 1 | setError('', 'entityFile is not exists.'); 27 | return false; 28 | } 29 | 30 | if (!$params['entityFileMd5']) { 31 | $params['entityFileMd5'] = md5_file($entityFile); 32 | } 33 | $params['entityFile'] = '@' . $entityFile; 34 | 35 | $response = $this->_dispatchRequest($name, array($params)); 36 | 37 | if (!$response) { 38 | $this->setError("", 'request falied!'); 39 | return false; 40 | } 41 | 42 | if (is_array($response) && $response['code']) { 43 | $this->setError($response['code'], $response['message']); 44 | return false; 45 | } 46 | 47 | unset($response['code'], $response['message']); 48 | 49 | return $response; 50 | } 51 | } -------------------------------------------------------------------------------- /src/QcloudApi/Module/Cloudaudit.php: -------------------------------------------------------------------------------- 1 | _serverHost = $module . '.api.qcloud.com'; 16 | return $this; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/QcloudApi/Module/Partners.php: -------------------------------------------------------------------------------- 1 | _dispatchRequest($name, array($params)); 26 | 27 | if (!$response) { 28 | $this->setError("", 'request falied!'); 29 | return false; 30 | } 31 | 32 | if (is_array($response) && $response['code']) { 33 | $this->setError($response['code'], $response['message']); 34 | return false; 35 | } 36 | 37 | if ($params['script'] == 1) { 38 | return $response; 39 | } 40 | 41 | unset($response['code'], $response['message']); 42 | 43 | return $response; 44 | } 45 | } -------------------------------------------------------------------------------- /src/QcloudApi/Module/Snapshot.php: -------------------------------------------------------------------------------- 1 | setConfig($moduleConfig); 287 | } 288 | 289 | if ( isset($isMorphling) && $isMorphling ) { 290 | $moduleInstance->setServerHost(lcfirst($moduleName)); 291 | } 292 | 293 | return $moduleInstance; 294 | } 295 | } 296 | --------------------------------------------------------------------------------