itoprest.examples.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Shows a usage of the SOAP queries
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  23. */
  24. /**
  25. * Helper to execute an HTTP POST request
  26. * Source: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
  27. * originaly named after do_post_request
  28. */
  29. function DoPostRequest($sUrl, $aData, $sOptionnalHeaders = null)
  30. {
  31. // $sOptionnalHeaders is a string containing additional HTTP headers that you would like to send in your request.
  32. $sData = http_build_query($aData);
  33. $aParams = array('http' => array(
  34. 'method' => 'POST',
  35. 'content' => $sData,
  36. 'header'=> "Content-type: application/x-www-form-urlencoded\r\nContent-Length: ".strlen($sData)."\r\n",
  37. ));
  38. if ($sOptionnalHeaders !== null)
  39. {
  40. $aParams['http']['header'] .= $sOptionnalHeaders;
  41. }
  42. $ctx = stream_context_create($aParams);
  43. $fp = @fopen($sUrl, 'rb', false, $ctx);
  44. if (!$fp)
  45. {
  46. global $php_errormsg;
  47. if (isset($php_errormsg))
  48. {
  49. throw new Exception("Problem with $sUrl, $php_errormsg");
  50. }
  51. else
  52. {
  53. throw new Exception("Problem with $sUrl");
  54. }
  55. }
  56. $response = @stream_get_contents($fp);
  57. if ($response === false)
  58. {
  59. throw new Exception("Problem reading data from $sUrl, $php_errormsg");
  60. }
  61. return $response;
  62. }
  63. // If the library curl is installed.... use this function
  64. //
  65. function DoPostRequest_curl($sUrl, $aData)
  66. {
  67. $curl = curl_init($sUrl);
  68. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  69. curl_setopt($curl, CURLOPT_POST, true);
  70. curl_setopt($curl, CURLOPT_POSTFIELDS, $aData);
  71. $response = curl_exec($curl);
  72. curl_close($curl);
  73. return $response;
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////
  76. //
  77. // Main program
  78. //
  79. ////////////////////////////////////////////////////////////////////////////////
  80. // Define the operations to perform (one operation per call the rest service)
  81. //
  82. $aOperations = array(
  83. array(
  84. 'operation' => 'list_operations', // operation code
  85. ),
  86. array(
  87. 'operation' => 'core/create', // operation code
  88. 'comment' => 'Synchronization from blah...', // comment recorded in the change tracking log
  89. 'class' => 'UserRequest',
  90. 'output_fields' => 'id, friendlyname', // list of fields to show in the results (* or a,b,c)
  91. // Values for the object to create
  92. 'fields' => array(
  93. 'org_id' => "SELECT Organization WHERE name = 'Demo'",
  94. 'caller_id' => array('name' => 'monet', 'first_name' => 'claude'),
  95. 'title' => 'issue blah',
  96. 'description' => 'something happened'
  97. ),
  98. ),
  99. array(
  100. 'operation' => 'core/update', // operation code
  101. 'comment' => 'Synchronization from blah...', // comment recorded in the change tracking log
  102. 'class' => 'UserRequest',
  103. 'key' => 'SELECT UserRequest WHERE id=1',
  104. 'output_fields' => 'id, friendlyname, title', // list of fields to show in the results (* or a,b,c)
  105. // Values for the object to create
  106. 'fields' => array(
  107. 'title' => 'Issue #'.rand(0, 100),
  108. 'contacts_list' => array(
  109. array(
  110. 'role' => 'fireman #'.rand(0, 100),
  111. 'contact_id' => array('finalclass' => 'Person', 'name' => 'monet', 'first_name' => 'claude'),
  112. ),
  113. ),
  114. ),
  115. ),
  116. // Rewrite the full CaseLog on an existing UserRequest with id=1, setting date and user (optional)
  117. array(
  118. 'operation' => 'core/update',
  119. 'comment' => 'Synchronization from ServiceFirst', // comment recorded in the change tracking log
  120. 'class' => 'UserRequest',
  121. 'key' => 'SELECT UserRequest WHERE id=1',
  122. 'output_fields' => 'id, friendlyname, title',
  123. 'fields' => array(
  124. 'public_log' => array(
  125. 'items' => array(
  126. 0 => array(
  127. 'date' => '2001-02-01 23:59:59', //Allow to set the date of a true event, an alarm for eg.
  128. 'user_login' => 'Alarm monitoring', //Free text
  129. 'user_id' => 0, //0 is required for the user_login to be taken into account
  130. 'message' => 'This is 1st entry as an <b>HTML</b> formatted<br>text',
  131. ),
  132. 1 => array(
  133. 'date' => '2001-02-02 00:00:00', //If ommitted set automatically.
  134. 'user_login' => 'Alarm monitoring', //user=id=0 is missing so will be ignored
  135. 'message' => 'Second entry in text format:
  136. with new line, but format not specified, so treated as HTML!, user_id=0 missing, so user_login ignored',
  137. ),
  138. ),
  139. ),
  140. ),
  141. ),
  142. // Add a Text entry in the HTML CaseLog of the UserRequest with id=1, setting date and user (optional)
  143. array(
  144. 'operation' => 'core/update', // operation code
  145. 'comment' => 'Synchronization from Alarm Monitoring', // comment recorded in the change tracking log
  146. 'class' => 'UserRequest',
  147. 'key' => 1, // object id or OQL
  148. 'output_fields' => 'id, friendlyname, title', // list of fields to show in the results (* or a,b,c)
  149. // Example of adding an entry into a CaseLog on an existing UserRequest
  150. 'fields' => array(
  151. 'public_log' => array(
  152. 'add_item' => array(
  153. 'user_login' => 'New Entry', //Free text
  154. 'user_id' => 0, //0 is required for the user_login to be taken into account
  155. 'format' => 'text', //If ommitted, source is expected to be HTML
  156. 'message' => 'This text is not HTML formatted with 3 lines:
  157. new line
  158. 3rd and last line',
  159. ),
  160. ),
  161. ),
  162. ),
  163. array(
  164. 'operation' => 'core/get', // operation code
  165. 'class' => 'UserRequest',
  166. 'key' => 'SELECT UserRequest',
  167. 'output_fields' => 'id, friendlyname, title, contacts_list', // list of fields to show in the results (* or a,b,c)
  168. ),
  169. array(
  170. 'operation' => 'core/delete', // operation code
  171. 'comment' => 'Cleanup for synchro with...', // comment recorded in the change tracking log
  172. 'class' => 'UserRequest',
  173. 'key' => 'SELECT UserRequest WHERE org_id = 2',
  174. 'simulate' => true,
  175. ),
  176. array(
  177. 'operation' => 'core/apply_stimulus', // operation code
  178. 'comment' => 'Synchronization from blah...', // comment recorded in the change tracking log
  179. 'class' => 'UserRequest',
  180. 'key' => 1,
  181. 'stimulus' => 'ev_assign',
  182. // Values to set
  183. 'fields' => array(
  184. 'team_id' => 15, // Helpdesk
  185. 'agent_id' => 9 // Jules Verne
  186. ),
  187. 'output_fields' => 'id, friendlyname, title, contacts_list', // list of fields to show in the results (* or a,b,c)
  188. ),
  189. array(
  190. 'operation' => 'core/get_related', // operation code
  191. 'class' => 'Server',
  192. 'key' => 'SELECT Server',
  193. 'relation' => 'impacts', // relation code
  194. 'depth' => 4, // max recursion depth
  195. ),
  196. );
  197. $aOperations = array(
  198. array(
  199. 'operation' => 'core/create', // operation code
  200. 'comment' => 'Automatic creation of attachment blah blah...', // comment recorded in the change tracking log
  201. 'class' => 'Attachment',
  202. 'output_fields' => 'id, friendlyname', // list of fields to show in the results (* or a,b,c)
  203. // Values for the object to create
  204. 'fields' => array(
  205. 'item_class' => 'UserRequest',
  206. 'item_id' => 1,
  207. 'item_org_id' => 3,
  208. 'contents' => array(
  209. 'data' => 'iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAIAAAC0tAIdAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACmSURBVChTfZHRDYMwDESzQ2fqhHx3C3ao+MkW/WlnaFxfzk7sEnE6JHJ+NgaKZN2zLHVN2ssfkae0Da7FQ5PRk/ve4Hcx19Ie6CEGuh/6vMgNhwanHVUNbt73lUDbYJ+6pg8b3+m2RehsVPdMXyvQY+OVkB+Rrv64lUjb3nq+aCA6v4leRqtfaIgimr53atBy9PlfUhoh3fFCNDmErv9FWR6ylBL5AREbmHBnFj5lAAAAAElFTkSuQmCC',
  210. 'filename' => 'myself.png',
  211. 'mimetype' => 'image/png'
  212. ),
  213. ),
  214. ),
  215. array(
  216. 'operation' => 'core/get', // operation code
  217. 'class' => 'Attachment',
  218. 'key' => 'SELECT Attachment',
  219. 'output_fields' => '*',
  220. )
  221. );
  222. $aOperations = array(
  223. array(
  224. 'operation' => 'core/update', // operation code
  225. 'comment' => 'Synchronization from blah...', // comment recorded in the change tracking log
  226. 'class' => 'Server',
  227. 'key' => 'SELECT Server WHERE name="Server1"',
  228. 'output_fields' => 'id, friendlyname, description', // list of fields to show in the results (* or a,b,c)
  229. // Values for the object to create
  230. 'fields' => array(
  231. 'description' => 'Issue #'.time(),
  232. ),
  233. ),
  234. );
  235. $aOperations = array(
  236. array(
  237. 'operation' => 'core/create', // operation code
  238. 'comment' => 'Synchronization from blah...', // comment recorded in the change tracking log
  239. 'class' => 'UserRequest',
  240. 'output_fields' => 'id, friendlyname', // list of fields to show in the results (* or a,b,c)
  241. // Values for the object to create
  242. 'fields' => array(
  243. 'org_id' => "SELECT Organization WHERE name = 'Demo'",
  244. 'caller_id' => array('name' => 'monet', 'first_name' => 'claude'),
  245. 'title' => 'issue blah',
  246. 'description' => 'something happened'
  247. ),
  248. ),
  249. );
  250. $aXXXOperations = array(
  251. array(
  252. 'operation' => 'core/check_credentials', // operation code
  253. 'user' => 'admin',
  254. 'password' => 'admin',
  255. ),
  256. );
  257. $aOperations = array(
  258. array(
  259. 'operation' => 'core/delete', // operation code
  260. 'comment' => 'Cleanup for synchro with...', // comment recorded in the change tracking log
  261. 'class' => 'Server',
  262. 'key' => 'SELECT Server',
  263. 'simulate' => false,
  264. ),
  265. );
  266. if (false)
  267. {
  268. echo "Please edit the sample script and configure the server URL";
  269. exit;
  270. }
  271. else
  272. {
  273. $sUrl = "http://localhost/trunk/webservices/rest.php?version=1.1";
  274. }
  275. $aData = array();
  276. $aData['auth_user'] = 'no-export';
  277. $aData['auth_pwd'] = 'no-export';
  278. //$aData['auth_user'] = 'admin';
  279. //$aData['auth_pwd'] = 'admin';
  280. foreach ($aOperations as $iOp => $aOperation)
  281. {
  282. echo "======================================\n";
  283. echo "Operation #$iOp: ".$aOperation['operation']."\n";
  284. $aData['json_data'] = json_encode($aOperation);
  285. echo "--------------------------------------\n";
  286. echo "Input:\n";
  287. print_r($aOperation);
  288. $response = DoPostRequest($sUrl, $aData);
  289. $aResults = json_decode($response);
  290. if ($aResults)
  291. {
  292. echo "--------------------------------------\n";
  293. echo "Reply:\n";
  294. print_r($aResults);
  295. }
  296. else
  297. {
  298. echo "ERROR rest.php replied:\n";
  299. echo $response;
  300. }
  301. }
  302. ?>