itoprest.examples.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. );
  123. $sUrl = "http://localhost/rest-services/webservices/rest.php?version=1.0";
  124. $aData = array();
  125. $aData['auth_user'] = 'admin';
  126. $aData['auth_pwd'] = 'admin';
  127. foreach ($aOperations as $iOp => $aOperation)
  128. {
  129. echo "======================================\n";
  130. echo "Operation #$iOp: ".$aOperation['operation']."\n";
  131. $aData['json_data'] = json_encode($aOperation);
  132. echo "--------------------------------------\n";
  133. echo "Input:\n";
  134. print_r($aOperation);
  135. $response = DoPostRequest($sUrl, $aData);
  136. $aResults = json_decode($response);
  137. if ($aResults)
  138. {
  139. echo "--------------------------------------\n";
  140. echo "Reply:\n";
  141. print_r($aResults);
  142. }
  143. else
  144. {
  145. echo "ERROR rest.php replied:\n";
  146. echo $response;
  147. }
  148. }
  149. ?>