|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +$remote_user = 'test'; |
| 5 | +$remote_pass = 'apipassword'; |
| 6 | +$remote_url = 'https://yourserver.com:8080/remote/json.php'; |
| 7 | + |
| 8 | +function restCall($method, $data) { |
| 9 | + global $remote_url; |
| 10 | + |
| 11 | + if(!is_array($data)) return false; |
| 12 | + $json = json_encode($data); |
| 13 | + |
| 14 | + $curl = curl_init(); |
| 15 | + curl_setopt($curl, CURLOPT_POST, 1); |
| 16 | + |
| 17 | + if($data) curl_setopt($curl, CURLOPT_POSTFIELDS, $json); |
| 18 | + |
| 19 | + // needed for self-signed cert |
| 20 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
| 21 | + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); |
| 22 | + // end of needed for self-signed cert |
| 23 | + |
| 24 | + curl_setopt($curl, CURLOPT_URL, $remote_url . '?' . $method); |
| 25 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
| 26 | + |
| 27 | + $result = curl_exec($curl); |
| 28 | + curl_close($curl); |
| 29 | + |
| 30 | + return $result; |
| 31 | +} |
| 32 | + |
| 33 | +$result = restCall('login', array('username' => $remote_user, 'password' => $remote_pass, 'client_login' => false)); |
| 34 | +if($result) { |
| 35 | + $data = json_decode($result, true); |
| 36 | + if(!$data) die("ERROR!\n"); |
| 37 | + |
| 38 | + $session_id = $data['response']; |
| 39 | + |
| 40 | + $result = restCall('client_get', array('session_id' => $session_id, 'client_id' => array('username' => 'abcde'))); |
| 41 | + if($result) var_dump(json_decode($result, true)); |
| 42 | + else print "Could not get client_get result\n"; |
| 43 | + |
| 44 | + // or by id |
| 45 | + $result = restCall('client_get', array('session_id' => $session_id, 'client_id' => 2)); |
| 46 | + if($result) var_dump(json_decode($result, true)); |
| 47 | + else print "Could not get client_get result\n"; |
| 48 | + |
| 49 | + // or all |
| 50 | + $result = restCall('client_get', array('session_id' => $session_id, 'client_id' => array())); |
| 51 | + if($result) var_dump(json_decode($result, true)); |
| 52 | + else print "Could not get client_get result\n"; |
| 53 | + |
| 54 | + $result = restCall('logout', array('session_id' => $session_id)); |
| 55 | + if($result) var_dump(json_decode($result, true)); |
| 56 | + else print "Could not get logout result\n"; |
| 57 | +} |
0 commit comments