Example PHP Script - Print Uptime
Here is an example PHP script which uses the API to print the gateway's uptime. The completed script is available as an attachment on this page. Many thanks to the kind anonymous author for allowing me to post this example!
- 1 Requirements
- 2 Code
Requirements
PHP
cURL
Code
Download the full source code. -
Start a new PHP script. Configure the script for your particular gateway
<? Â Â Â Â $GATEWAY_IP = 'CHANGEME'; Â Â Â Â $USERNAME = 'admin'; Â Â Â Â $PASSWORD = 'admin'; Â Â Â Â Â $ch = curl_init(); Â Â Â Â Â $fields = array( Â Â Â Â Â Â Â Â 'admin_uid'Â Â Â Â Â => $USERNAME, Â Â Â Â Â Â Â Â 'admin_password' => $PASSWORD Â Â Â Â );
Log in to your gateway. This will save your authentication cookies in memory so that subsequent calls will be allowed.
curl_setopt($ch, CURLOPT_URL, "https://$GATEWAY_IP/admin/main.html"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_COOKIEFILE, ''); $result = curl_exec($ch); Â if ($result === false) { Â Â Â Â $error = curl_error($ch); Â Â Â Â curl_close($ch); Â Â Â Â Â die("Login failed: $error"); } Â if (preg_match("/Welcome,\\s+$USERNAME/i", $result) == 0 Â Â Â Â || preg_match("/Log Out/i", $result) == 0) { Â Â Â Â curl_close($ch); Â Â Â Â die("Login Failed!"); }
Build a call to the gateway.list method to get information about the gateway.
    $request = array(         'request' => array(             'method'    => 'gateway_list',             'parameters' => array()         )     );      $string = json_encode($request, JSON_FORCE_OBJECT);     $fields = array(         'request' => $string     );      curl_setopt($ch, CURLOPT_URL, "https://$GATEWAY_IP/json");     curl_setopt($ch, CURLOPT_POST, 1);     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);     curl_setopt($ch, CURLOPT_HEADER, false);     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);      $result = curl_exec($ch);      if ($result === false) {         $error = curl_error($ch);         curl_close($ch);          die("Request failed: $error");     }      curl_close($ch);      $response = json_decode($result);     $response_object = json_decode($response->response->result);     print $response_object->gateway->model_name; print "\n";     print "Uptime "; print $response_object->gateway->uptime; print "\n"; ?>
Â