{"id":1264,"date":"2018-10-07T20:57:34","date_gmt":"2018-10-07T20:57:34","guid":{"rendered":"http:\/\/www.penguino.co.uk\/?p=1264"},"modified":"2018-10-07T21:32:37","modified_gmt":"2018-10-07T21:32:37","slug":"ip-address-block-unblock-cloudflares-firewall-access-api-php-script","status":"publish","type":"post","link":"http:\/\/www.penguino.co.uk\/computing\/ip-address-block-unblock-cloudflares-firewall-access-api-php-script","title":{"rendered":"IP address block\/unblock via Cloudflare’s firewall access API using php script"},"content":{"rendered":"\n\n\r\n\n\n\n

\"\"<\/p>\n

Cloudflare has upgraded their API to version 4 some while ago.<\/p>\n

https:\/\/api.cloudflare.com\/#user-level-firewall-access-rule-properties<\/a><\/p>\n

I want to use the new APIs to block IP addresses using fail2ban. Using curl directly is a bit cumbersome, so I created a few php helper scripts. They can also be used in other projects.<\/p>\n

Thanks to andrieslouw<\/strong> at github https:\/\/gist.github.com\/andrieslouw\/3c833332cbf66f95ca6751f82013acf5<\/a> I didn\u2019t have to reinvent the wheel. He has created the backbones of the php scripts.<\/p>\n

I’ve gone a bit further – I added a search where by supplying an IP address and the script can locate the object ID and delete it from the Firewall access list.<\/p>\n

I also added some connection time-out and error checkings. They are not 100% nuclear-proof. Maybe you can help enhancing it.<\/p>\n

Please note I am using \u201cUser Level\u201d rules. On CF there are also Account and Organization level rule so you may need to amend the URL to suit your need.<\/p>\n

There are 3 scripts:<\/p>\n