Forum Discussion
PHP "Hello World" code to get and put data in a datagroup on LTM
Hello,
I am looking for a VERY basic iControl example using PHP that demonstrates how to get and put data in a datagroup on the LTM. Does anyone have any example code that can help me out?
Thank you very much!
3 Replies
- Arnaud_Lemaire
Employee
yep, i have the list one, need to find how to post it ...
- Kevin_Stewart
Employee
Hopefully this will post correctly in the forum.
Simple PHP datagroup GET function
// function dg_GET : get the current datagroup values // inputs: // $host = BIG-IP hostname // $datagroup = datagroup name // $adminpass = formatted admin name and password (ex. admin:admin) // output: // array of datagroup records function dg_GET($host, $datagroup, $adminpass) { // build the request $service_url = 'https://' . $host . '/mgmt/tm/ltm/data-group/internal/' . $datagroup; $ch = curl_init($service_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERPWD, $adminpass); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); // execute the request $response = curl_exec($ch); // format/strip the resulting records array $array = json_decode($response, true); $dgarray = $array['records']; curl_close($ch); return $dgarray; }To use the dg_GET function:
$dg_get = dg_GET('10.80.0.103', 'testdg', 'admin:password'); print_r($dg_get);Simple PHP datagroup PUT function
// function dg_PUT: add items to existing datagroup // inputs: // $host = BIG-IP hostname // $datagroup = datagroup name // $adminpass = formatted admin name and password (ex. admin:admin) // $arraydata = array-formatted recordset to pass to datagroup // output: // array of datagroup records function dg_PUT($host, $datagroup, $adminpass, $arraydata) { // call dg_GET to get the existing values $current_values = dg_GET($host, $datagroup, $adminpass); // build the request $service_url = 'https://' . $host . '/mgmt/tm/ltm/data-group/internal/' . $datagroup; $ch = curl_init($service_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERPWD, $adminpass); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); // format the datagroup records if ($current_values != "") { // append existing values to datastring array_push($current_values, $arraydata); $jsonstring = json_encode($current_values); } else { $jsonstring = '[' . json_encode($arraydata) . ']'; } //format the post payload $post = '{"name":"' . $datagroup . '","records":' . $jsonstring . '}'; // execure the request curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($post)) ); $response = curl_exec($ch); // format/strip the resulting records array $array = json_decode($response, true); $dgarray = $array['records']; curl_close($ch); return $dgarray; }To use the dg_PUT function
$test = array( "name" => "roger", "data" => "rabbit" ); $dg_post = dg_PUT('10.80.0.103', 'testdg', 'admin:password', $test); print_r($dg_post);Notice that the PUT function relies on the GET function to fetch existing values. iControlREST does not have the ability to add to an existing datagroup, so the PUT function must first get the existing values and then replace the entire recordset with the new values. Also note that there is absolutely no error checking in this sample code.
- Kevin_Stewart
Employee
The iControlREST API is available in 11.5. For anything earlier you have to use the SOAP-based iControl API.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com