Last active
June 14, 2016 15:49
-
-
Save wilpig/5cf4ed03116a3c642561ea22412c0376 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This here should really have some url encoding applied but this was a simple example and I couldn't be bothered.