I am working on updating my DNS via a PHP script. I've looked at the API documentation relating to cURL so I am trying to convert the cURL post to be PHP.
I have the following code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/<MY_ZONE>/dns_records/<MY_ID>");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
$fields = array();
$fields["X-Auth-Email"] = "[email protected]";
$fields["X-Auth-Key"] = "MY_KEY";
$fields["Content-Type"] = "application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $fields);
$dnsData = array();
$dnsData["id"] = "MY_ID";
$dnsData["type"] = "A";
$dnsData["name"] = "home";
$dnsData["content"] = $newIPAddress;
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($dnsData));
echo "posting to API<br />";
$result = curl_exec($ch);
echo "Result: " . $result;
With the above code I am getting the following response back from Cloudflare.
{"success":false,"errors":[{"code":6003,"message":"Invalid request headers","error_chain":[{"code":6100,"message":"Missing X-Auth-Email header"},{"code":6101,"message":"Missing X-Auth-Key header"},{"code":6105,"message":"Invalid Content-Type header, valid values are application/json,multipart/form-data"}]}],"messages":[],"result":null}
I've tried changing the json_encode to http_build_query instead but both return the same error.
i think you are misusing curl_setopt.
This is the correct way to do set multiple header:
curl_setopt($ch,CURLOPT_HTTPHEADER, ['HeaderName: HeaderValue','HeaderName2: HeaderValue2']);
EDIT
To make it more clear:
$headers = [
'X-Auth-Email: [email protected]',
'X-Auth-Key: MY_KEY',
'Content-Type: application/json'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
Headers are not key/value pair, but rather only values.
Also, you should send POST data with http_build_query().
The issue is that you're telling it to use application/json when you're passing it form data. Set your Content-type to multipart/form-data and it should work. If you want an example of how to use the JSON API, let me know.
EDIT: CURLOPT_HTTPHEADER does not accept key/values, only values. More information
$fields["X-Auth-Email"] = "[email protected]";
would therefore need to be changed to:
$fields[]= "X-Auth-Email: [email protected]";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With