Home » Php » json – Cloudflare PHP API dashboard

json – Cloudflare PHP API dashboard

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m using a curl request to get my Cloudflare dashboard data, but i am unsure how to use the JSON response to echo specific parts of the return response. I have tried the following code, but it gives me an error while trying to echo the specific part of the response.

<?php
$apikey = 'MY API KEY'; // Cloudflare Global API
$email = 'MY EMAIL'; // Cloudflare Email Adress
$domain = 'MY DOMAIN';  // zone_name // Cloudflare Domain Name
$zoneid = 'MY ZONE ID'; // zone_id // Cloudflare Domain Zone ID
// A-record oluşturur DNS sistemi için.
        $ch = curl_init("https://api.cloudflare.com/client/v4/zones/".$zoneid."/dns_records");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-Auth-Email: '.$email.'',
        'X-Auth-Key: '.$apikey.'',
        'Cache-Control: no-cache',
        // 'Content-Type: multipart/form-data; charset=utf-8',
        'Content-Type:application/json',
        'purge_everything: true'
        ));
        // -d curl parametresi.
        $data = array(
            'type' => 'A',
            'name' => ''.$dnsadgeldi.'',
            'content' => ''.$dnsipgeldi.'',
            'zone_name' => ''.$domain.'',
            'zone_id' => ''.$zoneid.'',
            'proxiable' => 'true',
            'proxied' => true,
            'ttl' => '120'
        );
        $data_string = json_encode($data);

        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
        //curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_string));
        $sonuc = curl_exec($ch);
        // If you want show output remove code slash.
        // print_r($sonuc);
        curl_close($ch);
        echo $data_string['1'];
?>
How to&Answers: