Cloudflare api v4 srv DNS creation failure in php


I am sending data as json with PHP Curl to create a dns record.

However, although I submit the data correctly, the Cloudflare API is returning a bug.

Example PHP SOURCE :


$ch2 = curl_init("".$zoneid."/dns_records");
		curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
		curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
		'X-Auth-Email: '.$email.'',
		'X-Auth-Key: '.$apikey.'',
		'Cache-Control: no-cache',
	    // 'Content-Type: multipart/form-data; charset=utf-8',
		'purge_everything: true'
		// -d curl parametresi.
		$srvdata = array(
		// Object Tipi
		  "type" => "SRV",
		  "service" => "_ts3",
		  "proto" => "_udp",
		  "name" => "{$dnsadgeldi}",
		  "priority" => 1,
		  "weight" => 5,
		  "port" => "{$dnsportgeldi}",
		  "target" => "{$dnsadgeldi}.{$domain}"		
		$srv_data_string = json_encode($srvdata);

		curl_setopt($ch2, CURLOPT_POST, true);
		curl_setopt($ch2, CURLOPT_POSTFIELDS, $srv_data_string);	

		$sonuc2 = curl_exec($ch2);		
		echo '<br> <h2> Error Log To Screen : 2 </h2> <br>';

Error Log :

{"success":false,"errors":[{"code":1004,"message":"DNS Validation Error","error_chain":[{"code":9101,"message":"service is a required data field."}]}],"messages":[],"result":null}

Is there anyone who can help solve this problem?

The service data already goes to the Cloudflare API, but the API does not seem to see it.

My eyes glazed over at the PHP. Can you echo the CURL command that’s going to the API? It looks like you included a service field, but that’s not what the error is saying.

Firstly thank you for reply.

The above php code now runs the curl command I wrote here and sends the data to the Cloudflare in json format.

Sending curl command Cloudflare api :

curl -X POST "" \
     -H "X-Auth-Email: [email protected]" \
     -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
     -H "Content-Type: application/json" \
     --data '{"type":"SRV","service":"_ts3","proto":"_udp","name":"testdnsname","priority":1,"weight":5,"port":6000,"target":"",}'

The error is returned when I submit:

The only issue I can see is a comma after the domain just before the }, but I don’t see that comma in your PHP source.

The only other thing I can suggest is to do it manually in the DNS settings page, and then check your Audit Log (Dashboard Home) and see what the Metadata looks like.

1 Like

I think the problem is in the Cloudflare itself.

I looked at the audit log and I get the same error when I send it in the same way.

New data in php :

	$srvdata = array(
    // Object Tipi || Veriler Alınıyor
	"zone_name" => "{$domain}",
	"zone_id" => "{$zoneid}",
	"type" => "SRV",
	"name" => "_ts3._udp.{$dnsadgeldi}.",
	"content" => "SRV 0 0 {$dnsportgeldi} {$dnsadgeldi}.{$domain}.",
	"priority" => 0,
	"weight" => 0,
	"port" => $dnsportgeldi,
	"target" => "{$dnsadgeldi}.{$domain}",
	"service" => "_ts3",
	"proto" => "_udp",
	"name" =>"{$dnsadgeldi}",
	"proxied" => false,
	"proxiable" => false,
	"ttl" => 1

Error Log :

{"success":false,"errors":[{"code":1004,"message":"DNS Validation Error","error_chain":[{"code":9101,"message":"service is a required data field."}]}],"messages":[],"result":null}

Curl Data :

{"zone_name":"","zone_id":"7040902b675a6e44233505615faaac01","type":"SRV","name":"zirve","content":"SRV 0 0 6000","priority":0,"weight":0,"port":"6000","target":"","service":"_ts3","proto":"_udp","proxied":false,"proxiable":false,"ttl":1}

It looks like it’s time to open a Support ticket: login to Cloudflare and then contact Cloudflare Support.

1 Like

Cloudflare support very slow, i fixed this issue. SRV Record need additionally data :slight_smile:

New Data in PHP :

	$srvdata = array(
	'type' => 'SRV',
	'data' => array(
1 Like

How would i write this down as json?
I currently have
{ "type":"SRV", "data": [{"name":${subdomain}, "target": "", "ttl": 120, "weight": 5, "priority": 0,"service":"_minecraft","proto":"_tcp","port":port}],"proxied":false};

But im still getting service is a required data field

var srvbody = { "type":"SRV","name": subdomain + "","data": {"name":${subdomain}, "target": "", "ttl": 120, "weight": 5, "priority": 0,"service":"_minecraft","proto":"_tcp","port":port},"proxied":false};
works, coulve been documented better