0

When trying to pass SSH key data to the digital ocean API, I am getting an error and I cannot figure out what to do here.

$data = array("name" => "TestDroplet", "region" => "nyc1", "size" => "1Gb", "image" => "TestImage", "ssh_keys":[1234567]);

This give the following error message:

Parse error: syntax error, unexpected ':', expecting ')'

Here is the full code:

$data = array("name" => "TestDroplet", "region" => "nyc1", "size" => "1Gb", "image" => "TestImage", "ssh_keys":[1234567]);
$data_string = json_encode($data);
$ch = curl_init('https://api.digitalocean.com/v2/droplets');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer TOKENHIDDEN',
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
print_r($result);
exit;

This does otherwise work great when I don't try to specify the SSH key. Unfortunately the image I am trying to build from requires the SSH key.

Here is the Digital Ocean API documentation which tells that the key is provided as an array: https://developers.digitalocean.com/documentation/v2/#create-a-new-droplet

Is the issue a simple matter of syntax or is it because I am trying to put an array inside an array?

Thanks

Atomiklan
  • 5,164
  • 11
  • 40
  • 62

0 Answers0