CDN cache configuration and resource refresh example in PHP Tencent Cloud Server API interface docking
When using Tencent Cloud Server for function development, combined with CDN (Content Distribution Network ) can effectively improve website access speed and user experience. This article will introduce how to connect the Tencent Cloud API interface through PHP code to configure CDN caching rules and refresh specified resources.
To use CDN, you need to first activate the CDN service and obtain the API key (SecretId and SecretKey), and ensure that PHP is installed SDK (SDK tool officially provided by Tencent Cloud, used to communicate with Tencent Cloud API).
The following is a simple CDN cache configuration example. Assume that our domain name is www.example.com
, and we need to add static files ( Such as images, CSS and JS, etc.) Cache for 1 hour (3600 seconds):
require_once 'vendor/autoload.php'; // 引入PHP SDK use QcloudCosClient; // 引入腾讯云SDK命名空间 $secretId = 'your_secretId'; // 替换成自己的SecretId $secretKey = 'your_secretKey'; // 替换成自己的SecretKey $client = new Client(array('region' => 'ap-guangzhou', 'credentials' => array('secretId' => $secretId, 'secretKey' => $secretKey))); // 创建CDN客户端 $params = array( 'Action' => 'SetCdnConfig', // 设置CDN配置 'Domain' => 'www.example.com', // 要配置的域名 'Cache' => array( 'SimpleCache' => array( array( 'CacheType' => 'file', // 缓存类型为文件缓存 'CacheTime' => 3600, // 缓存时间为1小时(3600秒) 'FollowOrigin' => 0 // 不遵循源站设置的缓存策略 ) ) ) ); $response = $client->post('/', $params); // 发送请求 print_r($response); // 打印响应结果
The above code sets the CDN cache configuration by calling the SetCdnConfig
interface. The specific operations are as follows:
Action
to SetCdnConfig
, indicating that you want to set the CDN configuration. Domain
to the domain name that needs to be configured (here is the example domain name www.example.com
). Cache
is the cache configuration. Here, SimpleCache is used to set the file cache. The cache time is 1 hour (3600 seconds) and does not follow the cache policy set by the origin site. The following is a simple resource refresh example, assuming we need to refreshwww.example. An image under com
/images/example.png
:
require_once 'vendor/autoload.php'; // 引入PHP SDK use QcloudCosClient; // 引入腾讯云SDK命名空间 $secretId = 'your_secretId'; // 替换成自己的SecretId $secretKey = 'your_secretKey'; // 替换成自己的SecretKey $client = new Client(array('region' => 'ap-guangzhou', 'credentials' => array('secretId' => $secretId, 'secretKey' => $secretKey))); // 创建CDN客户端 $params = array( 'Action' => 'RefreshCdnUrl', // 刷新CDN资源 'Urls' => array( 'http://www.example.com/images/example.png' // 要刷新的资源URL ) ); $response = $client->post('/', $params); // 发送请求 print_r($response); // 打印响应结果
The above code refreshes CDN resources by calling the RefreshCdnUrl
interface. The specific operations are as follows:
Action
to RefreshCdnUrl
, indicating that CDN resources should be refreshed. Urls
to the resource URL to be refreshed. Here is an image under the example domain name www.example.com
. This article introduces how to configure CDN caching rules and refresh specified resources through PHP code to connect to the Tencent Cloud server API interface. I hope this article can help readers make better use of CDN to improve website access speed and user experience when using Tencent Cloud servers for development.
The above is the detailed content of Example of CDN cache configuration and resource refresh in PHP Tencent Cloud Server API interface docking. For more information, please follow other related articles on the PHP Chinese website!