How to Create Country Block Firewall Rule with API

firewall

#1

$postdata=’[
{
“paused”: false,
“description”: “Creat with API”,
“action”: “block”,

"filter": {
  "expression": "(ip.geoip.country ne "DZ")",
  "paused": false,
  "description": "not /api"
}

}]’;

$url = ‘https://api.cloudflare.com/client/v4/zones/’. $zoneid .’/firewall/rules’;

  $curl_options = [

        CURLOPT_VERBOSE => false,

        CURLOPT_FORBID_REUSE => true,

        CURLOPT_RETURNTRANSFER => 1,

        CURLOPT_HEADER => false,

        CURLOPT_TIMEOUT => 60,

        CURLOPT_SSL_VERIFYPEER => false,

        CURLOPT_FOLLOWLOCATION => true,

    ];

	

	

$headers = [

  'X-Auth-Email: ' . $email,

  'X-Auth-Key: ' . $api_key,

  'Content-type: application/json'

  

    ];

	




    $ch = curl_init();

    curl_setopt_array($ch, $curl_options);    

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); 

    $data = curl_exec($ch);

    $error = curl_error($ch);

    curl_close($ch);

    if (!empty($error)) {

        throw new Exception('Curl Error.');

    }

	$datatojson = json_decode($data,true);
	print_r($datatojson);

It show me :
Array ( [result] => [success] => [errors] => Array ( [0] => Array ( [code] => 10014 [message] => firewallrules.api.malformed_request_body ) ) [messages] => Array ( ) )

What 's wrong?


#2

i have my website https://empireict.net/ i want to add country API block, how can i do it the easy way. am not IT expert


#3

I would say the easy way would be login from a web browser and use the firewall tab.


closed #4

This topic was automatically closed after 30 days. New replies are no longer allowed.