ak86 / php-cloudflare-api-client

一个基于PHP的客户端库,简化Cloudflare API操作。

v1.0.6 2021-10-07 09:22 UTC

This package is auto-updated.

Last update: 2024-09-07 16:16:05 UTC


README

一个基于PHP的Cloudflare API客户端库,允许您直接通过PHP脚本执行常见的基于Cloudflare的操作,如清除网站缓存、将网站切换到开发模式等,而无需登录到Cloudflare仪表板。

需求

  • PHP 5.6或更高版本
  • Composer包管理器
  • 启用HTTPS的Web服务器。所有对Cloudflare API的请求都必须通过HTTPS发送。
  • Cloudflare API令牌。您可以从用户个人资料的API令牌页面获取API令牌,该页面位于https://dash.cloudflare.com/profile/api-tokens

注意:有两种方式可以验证Cloudflare API。较新的基于API令牌的验证或旧的基于API密钥的验证。如果您选择使用基于API令牌的验证,请确保以下权限已启用

  • cache_purge:edit
  • zone_settings:edit

安装

打开您的终端。切换到项目的根目录,并运行以下命令安装包

composer require ak86/php-cloudflare-api-client

基本用法

在您的脚本中

// require composer autoloader
require_once 'vendor/autoload.php';

// import the library
use Ak86\CF\ApiClient as CloudflareApiClient;

try {
	// Instantiate CloudflareApiClient by passing your cloudflare authentication token 
	$cfClient = new CloudflareApiClient('your_cloudflare_auth_token');

	// To clear the cache (i.e. abc.com)
	$res1 = $cfClient->clearCache('abc.com');

	if($res1)
	{
		echo 'Successfully cleared the cache of abc.com';
	}

	// To set a site to development mode (i.e. xyz.com)
	$res2 = $cfClient->enableDevMode('xyz.com');

	switch($res2['set'])
	{
		case 'now':
			echo 'Successfully switched xyz.com to the dev. mode. It will expire in another '. ($res2['time_remaining'] / 60) .' minutes.';
		break;

		case 'before':
			echo 'xyz.com is already switched to the development mode. It will expire in another '. ($res2['time_remaining'] / 60) .' minutes.';
		break;
	}
}
catch (Exception $e){
	// catch and handle exceptions here
	echo $e->getMessage();
	exit;
}

示例应用

请查看以下仓库,了解如何使用CloudflareApiClient的演示项目

https://github.com/amilak86/slack-cloudflare-api-interface

许可证

MIT

作者

Amila Kalansooriya