php [cloudflare api] cloudflare php api #security
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [cloudflare api] cloudflare php api #security相关的知识,希望对你有一定的参考价值。
# Get Cloudflare IPs
curl -X GET "https://api.cloudflare.com/client/v4/ips"
# get user info
curl -X GET "https://api.cloudflare.com/client/v4/user" \
-H "X-Auth-Email: user@example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json"
# edit user
curl -X PATCH "https://api.cloudflare.com/client/v4/user" \
-H "X-Auth-Email: user@example.com" \
-H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
-H "Content-Type: application/json" \
--data '{"first_name":"John","last_name":"Appleseed","telephone":"+1 123-123-1234","country":"US","zipcode":"12345"}'
##
## ZONE
##
# bulk add domains
# "jump_start" key will have Cloudflare automatically attempt to scan for common DNS records—e.g. "www", "mail", "blog" and many others
#-> so that you don't have to configure them by hand (you should still confirm we found them all).
#-> copy "name_servers" value and update the nameservers at your registrar
for domain in $(cat domains.txt); do \
curl -X POST -H "X-Auth-Key: $CF_API_KEY" -H "X-Auth-Email: $CF_API_EMAIL" \
-H "Content-Type: application/json" \
"https://api.cloudflare.com/client/v4/zones" \
--data '{account: {"id: "id_of_that_account"}, name":"'$domain'","jump_start":true}'; done
<?php
/*
https://github.com/cloudflare/cloudflare-php
composer require cloudflare/sdk
*/
$key = new Cloudflare\API\Auth\APIKey('kythuat.hoangweb@gmail.com', '413c410421189d575c73ee2da2a54893ee2e9');
$adapter = new Cloudflare\API\Adapter\Guzzle($key);
$user = new Cloudflare\API\Endpoints\User($adapter);
/**
get current user
*/
echo $user->getUserID();
echo $user->getUserEmail();
//get detail user
$userdt = $user->getUserDetails();
$userdt->id;
#or
$u = $adapter->get('user');
$userdt = json_decode($u->getBody())->result;
/**--------------------------------------------------------------------------------
Zones
*/
//list zones
$t = $adapter->get('zones',['status'=>'active','page'=>1,'per_page'=>20,'order'=>'status','match'=>'all']);
print_r(json_decode($t->getBody())->result);
//create zone
$t = $adapter->post('zones',[
'name'=>'hoangdata.com',
'jump_start'=> false
]);
$r = json_decode($t->getBody())->result;
echo $r['id']; //zone id
printf ("Please replace old name server %s with new %s",
join($r['original_name_servers'],', '),
join($r['name_servers'],', ')
);
//get zone
$t=$adapter->get('zones/8959eb328642c5dc3d843eea6ffb5d8f');
print_r(json_decode($t->getBody())->result);
//update zone: some params only for Business and Enterprise plans
$adapter->patch('zones/<zoneID>', [
'paused'=> false,
]);
//delete zone
$adapter->delete('zones/<id>');
//zone activation check (note: perform one per hour)
$t=$adapter->put('zones/<id>/activation_check');
print_r(json_decode($t->getBody())->result);
/*
Cache
*/
//Remove ALL files from Cloudflare's cache. Note: you can purge by url, tags..
$t=$adapter->post('zones/8959eb328642c5dc3d843eea6ffb5d8f/purge_cache', ['purge_everything'=>true]);
print_r(json_decode($t->getBody())->result);
/*------------------------------------------
Zone settings
*/
//Get all Zone settings
$t=$adapter->get('zones/8959eb328642c5dc3d843eea6ffb5d8f/settings');
$values = json_decode($t->getBody())->result;
foreach($values as $item) {
printf('id=%s, value=%s, editable=%s', $item->id, print_r($item->value,1), (int)$item->editable);
}
//get single zone setting,..ex:
$t=$adapter->get('zones/8959eb328642c5dc3d843eea6ffb5d8f/settings/always_online');
$t = json_decode($t->getBody())->result;print_r($t);
//edit zones
$r=$adapter->patch('zones/<id>/settings', [
'items'=> [
["id": "always_online","value": "on"],
["id": "browser_cache_ttl", "value": 18000]
]
]);
print_r($r);
//edit single zone setting
$adapter->patch('zones/<id>/settings/always_online', ["value":"on"]);
/**----------------------------------------------
DNS
*/
//List DNS Records
$t=$adapter->get('zones/8959eb328642c5dc3d843eea6ffb5d8f/dns_records');
print_r( $t = json_decode($t->getBody())->result);
//Create new DNS Record
$adapter->post('zones/<id>/dns_records', [
"type"=>"A","name"=>"example.com","content"=>"127.0.0.1","ttl"=>120,"priority"=>10,"proxied"=>false
]);
//DNS a Record Details
print_r $adapter->get('zones/<id>/dns_records/<id>');
//Update exist DNS Record
$adapter->patch('zones/<id>/dns_records/<id>', [
"type"=>"A","name"=>"example.com","content"=>"127.0.0.1","ttl"=>120,"priority"=>10,"proxied"=>false
]);
//Delete DNS Record
$adapter->delete('zones/<id>/dns_records/<id>');
/**----------------------------------------------
SSL
*/
/*
custom SSL: https://api.cloudflare.com/#custom-ssl-for-a-zone-properties
*/
$adapter->get('zones/<id>/custom_certificates');
/*
SSL verification
*/
//Get SSL Verification Info for a Zone
$t = $adapter->get('zones/<id>/ssl/verification', ['retry'=>true]);
/*
Universal SSL Settings: https://api.cloudflare.com/#universal-ssl-settings-for-a-zone-properties
*/
//Get Universal SSL Settings for a Zone
$r = $adapter->post('zones/<id>/ssl/universal/settings');
print_r($r);
//Edit Universal SSL Settings
$adapter->patch('zones/<id>/ssl/universal/settings', [
"enabled"=> true
]);
/**----------------------------------------------
Page rule for zone
*/
//List Available Page rule setting
print_r $adapter->get('zones/<id>/pagerules/setting');
//List Page Rules
$r = $adapter->get('zones/<id>/pagerules', [
'status'=>'active', 'order'=>'status','direction'=>'desc','match'=>'all'
]);
print_r($r);
//Create Page Rule
$adapter->post('zones/<id>/pagerules', [
"targets"=> [
["target"=>"url","constraint"=> ["operator"=>"matches","value"=>"*example.com/images/*"]]
],
"actions"=> [
["id"=>"always_online","value"=>"on"]
],
"priority"=>1,"status"=>"active"
]);
//get a Page Rule Details
$adapter->get('zones/<id>/pagerules/<id>');
//update page rule: Replace a page rule
$adapter->put('zones/<id>/pagerules/<id>', [
"targets"=> [
["target"=>"url","constraint"=> ["operator"=>"matches","value"=>"*example.com/images/*"]]
],
"actions"=> [
["id"=>"always_online","value"=>"on"]
],
"priority"=>1,"status"=>"active"
]);
//edit page rule
$adapter->patch('zones/<id>/pagerules/<id>', [
"targets"=> [
["target"=>"url","constraint"=> ["operator"=>"matches","value"=>"*example.com/images/*"]]
],
"actions"=> [
["id"=>"always_online","value"=>"on"]
],
"priority"=>1,"status"=>"active"
]);
//Delete Page Rule
$adapter->delete('zones/<id>/pagerules/<id>');
/**----------------------------------------------
Firewall rules: USER, account, zone
*/
/*
##### user level
*/
//List Access Rules
$r=$adapter->get('user/firewall/access_rules/rules', [
'page'=>1, 'per_page'=>20, 'mode'=>'challenge',
'configuration.target'=>'ip',
'configuration.value'=>'198.51.100.4',
'notes'=>'my note','match'=>'all','order'=>'mode','direction'=>'desc'
]);
print_r($r);
//Create Access Rule
//Make a new IP, IP range, or country access rule for all zones owned by the user.
$r=$adapter->post('user/firewall/access_rules/rules', [
"mode"=>"challenge",
"configuration"=> ["target"=>"ip","value"=>"198.51.100.4"],
"notes"=>"This rule is on because of an event that occured on date X"
]);
//Edit Access Rule
$adapter->patch('user/firewall/access_rules/rules/<id>', [
"mode"=>"challenge","notes"=>"This rule is on because of an event that occured on date X"
]);
//Delete Access Rule
$adapter->delete('user/firewall/access_rules/rules/<id>');
/*
##### account level
*/
//List Access Rules
print_r $adapter->get('accounts/<id>/firewall/access_rules/rules', [
'page'=>1,'per_page'=>20,'mode'=>'challenge',
'configuration.target'=>'ip',
'configuration.value'=>'198.51.100.4',
'notes'=>'my note','match'=>'all','order'=>'mode','direction'=>'desc'
]);
//Create Access Rule
$adapter->post('accounts/<id>/firewall/access_rules/rules', [
"mode"=>"challenge",
"configuration"=>["target"=>"ip","value"=>"198.51.100.4"],
"notes":"This rule is on because of an event that occured on date X"
]);
//Update Access Rule
$apdaper->patch('accounts/<id>/firewall/access_rules/rules/<id>', [
"id":"92f17202ed8bd63d69a66b86a49a8f6b","notes":"This rule is on because of an event that occured on date X","allowed_modes":["whitelist","block","challenge","js_challenge"],"mode":"challenge","configuration":{"target":"ip","value":"198.51.100.4"},"created_on":"2014-01-01T05:20:00.12345Z","modified_on":"2014-01-01T05:20:00.12345Z","scope":{"id":"01a7362d577a6c3019a474fd6f485823","name":"Demo Account","type":"account"}
]);
//Delete Access Rule
$adapter->delete('accounts/<id>/firewall/access_rules/rules/<id>');
/*
##### ZONE level
*/
//List Access Rules
$adapter->get('zones/:zone_id/firewall/access_rules/rules',[
"page": "1",
"per_page": "20",
"mode": "challenge",
"configuration.target": "ip",
"configuration.value": "198.51.100.4",
"notes": "my note",
"match": "all",
"order": "mode",
"direction": "desc"
]);
//Create Access Rule
$adapter->post('zones/:zone_id/firewall/access_rules/rules', [
"mode":"challenge","configuration":{"target":"ip","value":"198.51.100.4"},"notes":"This rule is on because of an event that occured on date X"
]);
//Delete Access Rule
$adapter->delete('zones/:zone_id/firewall/access_rules/rules/:identifier');
以上是关于php [cloudflare api] cloudflare php api #security的主要内容,如果未能解决你的问题,请参考以下文章
cloudflare api 将 curl 转换为 php curl 并发送 CNAME 更新
如何“动态”在 PHP 中向 cloudflare 添加子域?
如何对 Amazon API Gateway 终端节点进行 CNAME
在 https:// 中设置默认 Wordpress 并将其重定向到 http:// 并在 Cloudflare 中将 http:// 转发到 https://