注意:您的安全凭证代表您的账号身份和所拥有的权限,等同于您的登录密码,切勿泄露他人。
*.tencentcloudapi.com
,产品的调用地址有一定区别,例如,云服务器的调用地址为cvm.tencentcloudapi.com
。具体调用地址可参考对应产品的 API 文档。通过 Composer 获取安装是使用 PHP SDK 推荐方法,Composer 是 PHP 的依赖管理工具。关于 Composer 详细可参见 Composer 官网。
说明:Composer 需要 PHP 5.3.2+ 以上版本,且需要开启 openssl。
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
中国大陆地区的用户可以使用腾讯云镜像源提高下载速度,在打开的命令窗口执行以下命令:
composer config -g repos.packagist composer https://mirrors.tencent.com/composer/
在打开的命令窗口执行命令安装 SDK(安装到指定位置),例如安装到C:\Users\···>
目录下,则在指定的位置打开命令窗口,并执行以下命令:
composer require tencentcloud/tencentcloud-sdk-php
在代码中添加以下引用代码。注意:如下仅为示例,Composer 会在项目根目录下生成 vendor 目录,/path/to/ 为项目根目录的实际绝对路径(如果是在当前目录执行,可以省略绝对路径)。
require '/path/to/vendor/autoload.php';
说明:
- 如果只想安装某个产品的,可以使用
composer require tencentcloud/
产品名,例如composer require tencentcloud/cvm
。
下面以查询实例接口 DescribeInstances 为例:
<?php
require_once '/path/to/vendor/autoload.php';
use TencentCloud\Cvm\V20170312\Models\DescribeInstancesRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
try {
$cred = new Credential("secretId", "secretKey");
$client = new CvmClient($cred, "ap-guangzhou");
$req = new DescribeInstancesRequest();
$resp = $client->DescribeInstances($req);
print_r($resp->toJsonString());
}
catch(TencentCloudSDKException $e) {
echo $e;
}
您可以在 Github Examples 目录下找到更详细的示例。
如果是有代理的环境下,需要设置系统环境变量https_proxy
,否则可能无法正常调用,抛出连接超时的异常。 或者使用 GuzzleHttp 代理配置:
$cred = new Credential("secretId", "secretKey");
$httpProfile = new HttpProfile();
$httpProfile->setProxy('https://ip:port');
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new OcrClient($cred, 'ap-beijing', $this->clientProfile);
如果您的 PHP 环境证书有问题,可能会遇到报错,类似于cURL error 60: See http://curl.haxx.se/libcurl/c/libcurl-errors.html
,请尝试按以下步骤解决:
cacert.pem
,将其保存到 PHP 安装路径下。php.ini
文件,删除curl.cainfo
配置项前的分号注释符(;),值设置为保存的证书文件cacert.pem
的绝对路径。此 SDK 依赖的 GuzzleHttp 需要开启 php_curl 扩展,查看环境上的 php.ini 环境确认是否已启用,例如在 Linux 环境下,PHP 7.1 版本,托管在 apache 下的服务,可以打开 /etc/php/7.1/apache2/php.ini,查看 extension=php_curl.dll 配置项是否已被注释,请删除此项配置前的注释符并重启 apache。
命令行下执行正常,但是放在 Web 服务器执行则报错:
cURL error 0: The cURL request was retried 3 times and did not succeed. The most likely reason for the failure is that cURL was unable to rewind the body of the request and subsequent retries resulted in the same error. Turn on the debug option to see what went wrong. See https://bugs.php.net/bug.php?id=47204 for more information. (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
此问题出现情况不一。可以运行php -r "echo sys_get_temp_dir();"
,打印系统默认临时目录绝对路径,然后在php.ini
配置sys_temp_dir
为这个值,尝试是否能解决。
为了支持部分源码安装的需要,我们将依赖的包文件放在 vendor 目录中,又考虑到不能造成对 composer 的不兼容,github 不得不设置禁止导出 vendor 目录,造成必须使用git clone
命令才能拿到 vendor 目录的情况,对一些不熟悉 github 的用户造成了困扰。从3.0.188版本开始,我们暂时移除了源码安装,必须使用 composer 安装 SDK 和依赖的包。
本页内容是否解决了您的问题?