How to create dns in php using Cloudflare API?


#1

Hello,

Cloudflare, I would like to use the api system to create DNS records according to what the user will get.

I have already taken the API from the cloudflare API v1 page, but I have not been able to build the new system.

PHP How can I use curl commands in a page?

For example :

curl -X POST “http s:// api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records
-H “X-Auth-Email: [email protected] example . com”
-H “X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41”
-H “Content-Type: application / json”
–data '{“type”: “name”: “example . com”, “content”: “127.0.0.1”, “ttl”: 120, “priority”: 10, “proxied”: false }

How can I use these codes in the php software language, how can I print positive or negative results?


#2

Cloudflare API v1 was deprecated a while back, you should be using v4.

I’d also say that this query is more of a general PHP developer question than a Cloudflare specific one - namely ‘How do I call an API from within PHP’. You might get better info from a Google search of StackOverflow etc. than on here which is more ‘sysadmin’ oriented really.

Having said that, sure someone will chip in with some advice for you.


#3

Hello there,

Thank you for reply, problem solved :

PHP CODE FOR CREATE DNS RECORD:

<?php
    /* Cloudflare.com | APİv4 | Api Ayarları */
    $apikey = 'apikey'; // Cloudflare Global API
    $email = 'mailhere'; // Cloudflare Email Adress
    $domain = 'domainhere';  // zone_name // Cloudflare Domain Name
    $zoneid = 'zoneidhere'; // 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);
?>