Skip to content

Instantly share code, notes, and snippets.

@wilpig
Last active June 14, 2016 15:49
Show Gist options
  • Save wilpig/5cf4ed03116a3c642561ea22412c0376 to your computer and use it in GitHub Desktop.
Save wilpig/5cf4ed03116a3c642561ea22412c0376 to your computer and use it in GitHub Desktop.
<?php
// Here's some data
$fields=array( "Height" => "5",
"Cabinet" => "76",
"Label" => "Test_Api",
"DeviceType" => "Chassis",
"Owner" => "23",
"Position" => "5"
);
//url-ify the data for the POST
function stringify($fields){
$fields_string="";
// Sanitize your data here
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
return $fields_string;
}
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "http://dev.opendcim.org/api/v1/device/${fields['Label']}");
curl_setopt($ch, CURLOPT_USERPWD, "dcim:dcim");
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, stringify($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
// $output contains the output string
$output = curl_exec($ch);
print_r($output);
// close curl resource to free up system resources
curl_close($ch);
@wilpig
Copy link
Author

wilpig commented Jun 14, 2016

This here should really have some url encoding applied but this was a simple example and I couldn't be bothered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment