Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cloudflare V4 PHP API - block / unblock IP in firewall
<?php
/**
* @author https://github.com/andrieslouw
* @copyright 2016
**/
function cfban($ipaddr){
$cfheaders = array(
'Content-Type: application/json',
'X-Auth-Email: your@email.com',
'X-Auth-Key: yourauthkeyhere'
);
$data = array(
'mode' => 'block',
'configuration' => array('target' => 'ip', 'value' => $ipaddr),
'notes' => 'Banned on '.date('Y-m-d H:i:s').' by PHP-script'
);
$json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $cfheaders);
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules');
$return = curl_exec($ch);
curl_close($ch);
if ($return === false){
return false;
}else{
$return = json_decode($return,true);
if(isset($return['success']) && $return['success'] == true){
return $return['result']['id'];
}else{
return false;
}
}
}
function cfunban($block_rule_id){
$cfheaders = array(
'Content-Type: application/json',
'X-Auth-Email: your@email.com',
'X-Auth-Key: yourauthkeyhere'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $cfheaders);
curl_setopt($ch, CURLOPT_URL, 'https://api.cloudflare.com/client/v4/user/firewall/access_rules/rules/'.$block_rule_id);
$return = curl_exec($ch);
curl_close($ch);
if ($return === false){
return false;
}else{
$return = json_decode($return,true);
if(isset($return['success']) && $return['success'] == true){
return $return['result']['id'];
}else{
return false;
}
}
}
?>
@jruzafa

This comment has been minimized.

Copy link

@jruzafa jruzafa commented Nov 28, 2016

Works perfect!! thanks :-)

@dk-wamp

This comment has been minimized.

Copy link

@dk-wamp dk-wamp commented Nov 30, 2017

why doesn't works if I set a range ?
{"success":false,"errors":[{"code":1001,"message":"Invalid request. Configuration value was not a valid IP address"}],"messages":[],"result":null}

Is there a way to use a range ?

@llbbl

This comment has been minimized.

Copy link

@llbbl llbbl commented Aug 13, 2018

For others landing here, the official PHP SDK is here and its pretty great.

https://github.com/cloudflare/cloudflare-php

Examples:
https://support.cloudflare.com/hc/en-us/articles/115001661191

@ajoudeh

This comment has been minimized.

Copy link

@ajoudeh ajoudeh commented Aug 28, 2018

This block IP for all sites, is it? and how can i block IP for Specific WebSite?

@jeanphillippe

This comment has been minimized.

Copy link

@jeanphillippe jeanphillippe commented Apr 22, 2019

I'm still looking for the same thing, did you found a solution?

@Dilpreetsinghmann

This comment has been minimized.

Copy link

@Dilpreetsinghmann Dilpreetsinghmann commented Oct 28, 2019

I want to use your script but I am a nobe at scripting. After playing around with your script didn't reach anywhere. I want to build a script to ban Ip based on its rpm on my server and eventually removing the ban after a while say 600 seconds or 20 min. using Cloudflare and any other scan script. The idea behind is to block any DDoS attack.

@achernar

This comment has been minimized.

Copy link

@achernar achernar commented Apr 4, 2020

where I get the block_rule_id?

@edglev

This comment has been minimized.

Copy link

@edglev edglev commented Apr 10, 2020

where I get the block_rule_id?

cfban function returns id on succesful ban

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment