itoprest.examples.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. array(
  117. 'operation' => 'core/get', // operation code
  118. 'class' => 'UserRequest',
  119. 'key' => 'SELECT UserRequest',
  120. 'output_fields' => 'id, friendlyname, title, contacts_list', // list of fields to show in the results (* or a,b,c)
  121. ),
  122. array(
  123. 'operation' => 'core/delete', // operation code
  124. 'comment' => 'Cleanup for synchro with...', // comment recorded in the change tracking log
  125. 'class' => 'UserRequest',
  126. 'key' => 'SELECT UserRequest WHERE org_id = 2',
  127. 'simulate' => true,
  128. ),
  129. array(
  130. 'operation' => 'core/apply_stimulus', // operation code
  131. 'comment' => 'Synchronization from blah...', // comment recorded in the change tracking log
  132. 'class' => 'UserRequest',
  133. 'key' => 1,
  134. 'stimulus' => 'ev_assign',
  135. // Values to set
  136. 'fields' => array(
  137. 'team_id' => 15, // Helpdesk
  138. 'agent_id' => 9 // Jules Verne
  139. ),
  140. 'output_fields' => 'id, friendlyname, title, contacts_list', // list of fields to show in the results (* or a,b,c)
  141. ),
  142. array(
  143. 'operation' => 'core/get_related', // operation code
  144. 'class' => 'Server',
  145. 'key' => 'SELECT Server',
  146. 'relation' => 'impacts', // relation code
  147. 'depth' => 4, // max recursion depth
  148. ),
  149. );
  150. $aOperations = array(
  151. array(
  152. 'operation' => 'core/create', // operation code
  153. 'comment' => 'Automatic creation of attachment blah blah...', // comment recorded in the change tracking log
  154. 'class' => 'Attachment',
  155. 'output_fields' => 'id, friendlyname', // list of fields to show in the results (* or a,b,c)
  156. // Values for the object to create
  157. 'fields' => array(
  158. 'item_class' => 'UserRequest',
  159. 'item_id' => 1,
  160. 'item_org_id' => 3,
  161. 'contents' => array(
  162. 'data' => 'iVBORw0KGgoAAAANSUhEUgAAAA8AAAAPCAIAAAC0tAIdAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACmSURBVChTfZHRDYMwDESzQ2fqhHx3C3ao+MkW/WlnaFxfzk7sEnE6JHJ+NgaKZN2zLHVN2ssfkae0Da7FQ5PRk/ve4Hcx19Ie6CEGuh/6vMgNhwanHVUNbt73lUDbYJ+6pg8b3+m2RehsVPdMXyvQY+OVkB+Rrv64lUjb3nq+aCA6v4leRqtfaIgimr53atBy9PlfUhoh3fFCNDmErv9FWR6ylBL5AREbmHBnFj5lAAAAAElFTkSuQmCC',
  163. 'filename' => 'myself.png',
  164. 'mimetype' => 'image/png'
  165. ),
  166. ),
  167. ),
  168. array(
  169. 'operation' => 'core/get', // operation code
  170. 'class' => 'Attachment',
  171. 'key' => 'SELECT Attachment',
  172. 'output_fields' => '*',
  173. )
  174. );
  175. $aOperations = array(
  176. array(
  177. 'operation' => 'core/update', // operation code
  178. 'comment' => 'Synchronization from blah...', // comment recorded in the change tracking log
  179. 'class' => 'Server',
  180. 'key' => 'SELECT Server WHERE name="Server1"',
  181. 'output_fields' => 'id, friendlyname, description', // list of fields to show in the results (* or a,b,c)
  182. // Values for the object to create
  183. 'fields' => array(
  184. 'description' => 'Issue #'.time(),
  185. ),
  186. ),
  187. );
  188. $aOperations = array(
  189. array(
  190. 'operation' => 'core/create', // operation code
  191. 'comment' => 'Synchronization from blah...', // comment recorded in the change tracking log
  192. 'class' => 'UserRequest',
  193. 'output_fields' => 'id, friendlyname', // list of fields to show in the results (* or a,b,c)
  194. // Values for the object to create
  195. 'fields' => array(
  196. 'org_id' => "SELECT Organization WHERE name = 'Demo'",
  197. 'caller_id' => array('name' => 'monet', 'first_name' => 'claude'),
  198. 'title' => 'issue blah',
  199. 'description' => 'something happened'
  200. ),
  201. ),
  202. );
  203. $aXXXOperations = array(
  204. array(
  205. 'operation' => 'core/check_credentials', // operation code
  206. 'user' => 'admin',
  207. 'password' => 'admin',
  208. ),
  209. );
  210. $aOperations = array(
  211. array(
  212. 'operation' => 'core/delete', // operation code
  213. 'comment' => 'Cleanup for synchro with...', // comment recorded in the change tracking log
  214. 'class' => 'Server',
  215. 'key' => 'SELECT Server',
  216. 'simulate' => false,
  217. ),
  218. );
  219. if (false)
  220. {
  221. echo "Please edit the sample script and configure the server URL";
  222. exit;
  223. }
  224. else
  225. {
  226. $sUrl = "http://localhost/trunk/webservices/rest.php?version=1.1";
  227. }
  228. $aData = array();
  229. $aData['auth_user'] = 'no-export';
  230. $aData['auth_pwd'] = 'no-export';
  231. //$aData['auth_user'] = 'admin';
  232. //$aData['auth_pwd'] = 'admin';
  233. foreach ($aOperations as $iOp => $aOperation)
  234. {
  235. echo "======================================\n";
  236. echo "Operation #$iOp: ".$aOperation['operation']."\n";
  237. $aData['json_data'] = json_encode($aOperation);
  238. echo "--------------------------------------\n";
  239. echo "Input:\n";
  240. print_r($aOperation);
  241. $response = DoPostRequest($sUrl, $aData);
  242. $aResults = json_decode($response);
  243. if ($aResults)
  244. {
  245. echo "--------------------------------------\n";
  246. echo "Reply:\n";
  247. print_r($aResults);
  248. }
  249. else
  250. {
  251. echo "ERROR rest.php replied:\n";
  252. echo $response;
  253. }
  254. }
  255. ?>