3

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.

3 Answers 3

7

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().

Sign up to request clarification or add additional context in comments.

Comments

1

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]";

Comments

0

I have tested this code and i think you can use it without any problem. example:

    $request_headers = array(
    'X-Auth-Email: [email protected]',
    'X-Auth-Key: d820fa8fc881921323e08a2c19b8347896ac26',
    'Content-Type: application/json'
);

then:

curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.