How to delete dns in php using Cloudflare API in php?

#1

Hello, I would like to delete a record dns but I can not, only the recording works, I try this:

        		$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, "DELETE");
        		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' => ''.$type.'',
        			'name' => ''.$domainreg.'',
        			'content' => ''.$ip.'',
        			'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);

Do you have a idea ? Thx

#2

Your setup does not match the expected parameters outlined at https://api.cloudflare.com/#dns-records-for-a-zone-delete-dns-record

You could also use the abstracted API from https://support.cloudflare.com/hc/en-us/articles/115001661191-Cloudflare-PHP-API-Binding instead of composing the network request yourself.

1 Like
#3

Thank you for the answer! I looked through some of the docs but i still don’t quite understand what I am doing wrong… I’m trying to use this curl in PHP. Could you be more specific about what the error might be?
Thank you in advance, and have a good evening!

#4

Well, for starters, check out the URL you are calling, in addition to the data object you are sending.

2 Likes
#5

i try this code, but it does not work
$ch = curl_init("https://api.cloudflare.com/client/v4/zones/".$zoneid."/dns_records/".".$name);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “DELETE”);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘X-Auth-Email: ‘.$email.’’,
‘X-Auth-Key: ‘.$apikey.’’,
‘Content-Type:application/json’

        		));
        		$sonuc = curl_exec($ch);
        		curl_close($ch);

$name is name of subdomain, but, this code not work. I absolutely do not understand where can come for error.

I have this repsonse :
{“success”:false,“errors”:[{“code”:7003,“message”:“Could not route to /zones/MY_API_KEY/dns_records/subdomainname, perhaps your object identifier is invalid?”},{“code”:7000,“message”:“No route for that URI”}],“messages”:,“result”:null}1

#6

Try following the docs on this page.

The endpoint is:
DELETE zones/:zone_identifier/dns_records/:identifier

Both :zone_identifier and :identifier are ID’s. Not an API key, and a string as you suggest in the response.