Home > Backend Development > PHP Tutorial > Example of CDN cache configuration and resource refresh in PHP Tencent Cloud Server API interface docking

Example of CDN cache configuration and resource refresh in PHP Tencent Cloud Server API interface docking

PHPz
Release: 2023-07-06 21:04:01
Original
1256 people have browsed it

CDN cache configuration and resource refresh example in PHP Tencent Cloud Server API interface docking

1. Background

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.

2. CDN cache configuration

1. Preparation

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).

2. CDN cache configuration example

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); // 打印响应结果
Copy after login

The above code sets the CDN cache configuration by calling the SetCdnConfig interface. The specific operations are as follows:

  • Introduce PHP SDK and instantiate a CDN client.
  • Set Action to SetCdnConfig, indicating that you want to set the CDN configuration.
  • Set Domain to the domain name that needs to be configured (here is the example domain name www.example.com).
  • SettingsCache 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.
  • Send the request and print the response result.

3. Resource refresh

1. Resource refresh example

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); // 打印响应结果
Copy after login

The above code refreshes CDN resources by calling the RefreshCdnUrl interface. The specific operations are as follows:

  • Introduce PHP SDK and instantiate a CDN client.
  • Set Action to RefreshCdnUrl, indicating that CDN resources should be refreshed.
  • Set Urls to the resource URL to be refreshed. Here is an image under the example domain name www.example.com.
  • Send the request and print the response result.

4. Summary

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template