0

I am trying to send POST requests to Digitalocean's API v2 with cURL and custom headers but it is not working. I don't get any response, or the output/response I get is only:

Response from API:

My php code is:

<?php
$TOKEN = "digitalocean api token";

$headers = array("Authorization: Bearer $TOKEN","Content-Type: application/json",);

 $name = "Test"; //droplet name
 $region = "nyc2"; //region
 $size = "512mb"; //size
 $image = "303022"; //replace it
 $user_data = "#!/bin/bash apt-get install nginx -y";

$postData = array(
'name' => $name,
'region' => $region,
'size' => $size,
'image' => $image,
'user_data' => $user_data,);

$post_body = '';
foreach($postData as $key => $value) {
$post_body.= urlencode($key) . '=' . urlencode($value) . '&';
}
$post_body = rtrim($post_body, '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://api.digitalocean.com/v2/droplets");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
$result = curl_exec($ch);

echo "Response from API: $result";
?>

Please tell me what's wrong in it? I have error reporting enabled.

12
  • 1
    Why aren't you simply passing the $postData array to curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);? No need to process it that way. You're also missing a curl_close call at the end, and you really should be passing the authentication stuff as CURL options, not in the headers array (see the manual for the options that allow you to do all this) Commented Oct 30, 2014 at 16:14
  • Don't try to encode the post data yourself. Use PHP's built-in http_build_query function for that: curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData)); Commented Oct 30, 2014 at 16:15
  • The api docs of digitalocean mentions to use headers for auth. and curl_close() isn't necessary Commented Oct 30, 2014 at 16:16
  • @EliasVanOotegem: Note that with CURLOPT_POSTFIELDS: If value is an array, the Content-Type header will be set to multipart/form-data. Commented Oct 30, 2014 at 16:17
  • 1
    @AppfestiveReports: I've already pointed the header issue out in my answer (and in one of the comments above) Commented Oct 30, 2014 at 16:28

1 Answer 1

1

After looking at the $headers array more closely, I noticed you're sending your content as application/json, but at the same time, the postfields value does not match that content type. You'll probably have to add this:

$postFields = json_encode($postData);
$headers[] = 'Content-Length: ' . strlen($postFields));
//set your opts
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);//json encoded string

If that doesn't work, add the result of

var_dump(curl_getinfo($ch));

To your question, that'll tell you everything there is to know about the status of your curl request.

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

2 Comments

I don't know if Content-Length is really necessary.
@RocketHazmat: I can guarantee you it isn't. I'm just a bit anal about some things, and setting length headers is just one of the things I tend to do.

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.