url: ' . $url . ' / req: ' . $req; // .' hdr0: ' . $requestHeaders[0] .' hdr1: ' . $requestHeaders[1] .'

'; $ret_info = curl_exec($ch); $realsize=strlen($ret_info); if (curl_errno($ch)) { $err = 1; } else { if ($d) { echo '
ok: ' . $ret_info . '

'; } } curl_close($ch); if ($err) { return 0; } else { return 1; } } $cmd = $_POST["cmd"]; $p1 = $_POST["p1"]; $p2 = $_POST["p2"]; $p3 = $_POST["p3"]; $p4 = $_POST["p4"]; if ( !strlen($cmd)) { // authenticate form echo '

Power switch API test & sample PHP code




     Authenticate and get session key

.      Power switch mac address -      Power switch password -

         


'; // all other APIs form echo '


     All other APIs

.      * Power switch mac address p1 -      * Power switch session key p2 -

     Parameter p3 -      Parameter p4 -

   Select API command to call -

         


     Reference: Switch API description

     Test sample source - (rename .txt to .php)

'; goto ex; } // check p1 if (strlen($p1) != 12) { echo '
Invalid parameter - mac address

'; goto ex1; } // set API url $url = 'https://power.arstech.com/api.php'; // check command if ($cmd == '0') // authenticate goto a1; else if (($cmd >= '1') && ($cmd <= '5')) // all other goto a2; { echo '
Invalid parameter - mac address

'; goto ex1; } a1: // authenticate command $req = 'c=0&p1='. $p1. '&p2='. $p2; if ( !post_req( $url, $req)) { echo '
post error'; goto ex1; } echo '
return - '. $ret_info; if ($ret_info[0] == 'x') { echo '
authenticate error'; goto ex1; } echo '
use return value as a session key - $p2'; goto ex1; a2: // all other power switch commands // check p2 if (strlen($p2) != 6) { echo '
Invalid parameter - session key

'; goto ex1; } $req = 'c='. $cmd. '0&p1='. $p1. '&p2='. $p2; if ( strlen($p3)) $req .= '&p3='. $p3; if ( strlen($p4)) $req .= '&p4='. $p4; if ( !post_req( $url, $req)) { echo '
post error'; goto ex1; } echo '
return - '. $ret_info; if ($ret_info[0] == 'x') { echo '
API error'; goto ex1; } echo '
check return values and/or action performed'; goto ex1; ex1: echo '

Go back '; ex: ; // exit ?>