CloudFlare API and WordPress

CloudFlare protects and accelerates any website online. Once your website is a part of the CloudFlare community, its web traffic is routed through our intelligent global network. We automatically optimize the delivery of your web pages so your visitors get the fastest page load times and best performance. We also block threats and limit abusive bots and crawlers from wasting your bandwidth and server resources.
www.cloudflare.com

You’ll find the CloudFlare API at https://www.cloudflare.com/docs/client-api.html

Syntax:

define('CLOUDFLARE_APIEMAIL','emailadr'); // replace emailadr with your api email
define('CLOUDFLARE_APIKEY','abc'); // replace abc with your api key
define('APIURL','https://www.cloudflare.com/api_json.html');

$response = wp_remote_request(APIURL, array(
    'method'    => 'POST',
    'timeout'   => 60,
    'sslverify' => false,
    'body'      => array(
         'email'    => CLOUDFLARE_APIEMAIL
        ,'tkn'      => CLOUDFLARE_APIKEY
        ,'a'        => 'ACTION' // see https://www.cloudflare.com/docs/client-api.html actions ('a') and parameters
        ,'param1'   => ''
        ,'paramN'   => ''
    )
));

if (is_wp_error( $response )) {
   echo 'Something went wrong! <br />';
} else {
    $json = json_decode( wp_remote_retrieve_body($response), true); // true = return an assoc array  
    print_r($json);  
}

Example:

define('CLOUDFLARE_APIEMAIL','emailadr'); // replace emailadr with your api email
define('CLOUDFLARE_APIKEY','abc'); // replace abc with your api key
define('APIURL','https://www.cloudflare.com/api_json.html');

function is_ip_ok($ip) {
    global $errmsg;
    $errmsg = '';
    $response = wp_remote_request(APIURL, array(
        'method' => 'POST',
        'timeout' => 60,
        'sslverify' => false,
        'body' =>  array(
             'email' => CLOUDFLARE_APIEMAIL
            ,'tkn'   => CLOUDFLARE_APIKEY
            ,'a'     => 'ip_lkup'
            ,'ip'    =>  $ip
        )
    ));
    if (is_wp_error( $response )) {
       $errmsg = $response->get_error_message();
       return true; // don't block if is_ip_ok fails
    } else {
        $json = json_decode( wp_remote_retrieve_body($response), true); // true = return an assoc array
        if ($json['result'] != 'success') {
            $errmsg = $json['msg'];
            return true; // don't block if is_ip_ok fails
        } else {
            if ($json['response'][$ip] == "") {
                return true;
            } else {
                return false;
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">