itopsoap.examples.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. require_once('itopsoaptypes.class.inc.php');
  25. $sItopRoot = 'http'.((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS']!='off')) ? 's' : '').'://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].dirname($_SERVER['SCRIPT_NAME']).'/..';
  26. $sWsdlUri = $sItopRoot.'/webservices/itop.wsdl.php';
  27. //$sWsdlUri .= '?service_category=';
  28. $aSOAPMapping = SOAPMapping::GetMapping();
  29. ini_set("soap.wsdl_cache_enabled","0");
  30. $oSoapClient = new SoapClient(
  31. $sWsdlUri,
  32. array(
  33. 'trace' => 1,
  34. 'classmap' => $aSOAPMapping, // defined in itopsoaptypes.class.inc.php
  35. )
  36. );
  37. try
  38. {
  39. // The most simple service, returning a string
  40. //
  41. $sServerVersion = $oSoapClient->GetVersion();
  42. echo "<p>GetVersion() returned <em>$sServerVersion</em></p>";
  43. // More complex ones, returning a SOAPResult structure
  44. // (run the page to know more about the returned data)
  45. //
  46. $oRes = $oSoapClient->CreateIncidentTicket
  47. (
  48. 'admin', /* login */
  49. 'admin', /* password */
  50. 'Email server down', /* title */
  51. 'HW found shutdown', /* description */
  52. null, /* caller */
  53. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Demo'))), /* customer */
  54. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'NW Management'))), /* service */
  55. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'Troubleshooting'))), /* service subcategory */
  56. '', /* product */
  57. new SOAPExternalKeySearch(array(new SOAPSearchCondition('name', 'NW support'))), /* workgroup */
  58. array(
  59. new SOAPLinkCreationSpec(
  60. 'Device',
  61. array(new SOAPSearchCondition('name', 'switch01')),
  62. array()
  63. ),
  64. new SOAPLinkCreationSpec(
  65. 'Server',
  66. array(new SOAPSearchCondition('name', 'dbserver1.demo.com')),
  67. array()
  68. ),
  69. ), /* impacted cis */
  70. '1', /* impact */
  71. '1' /* urgency */
  72. );
  73. echo "<p>CreateIncidentTicket() returned:\n";
  74. echo "<pre>\n";
  75. print_r($oRes);
  76. echo "</pre>\n";
  77. echo "</p>\n";
  78. $oRes = $oSoapClient->SearchObjects
  79. (
  80. 'admin', /* login */
  81. 'admin', /* password */
  82. 'SELECT URP_Profiles' /* oql */
  83. );
  84. echo "<p>SearchObjects() returned:\n";
  85. if ($oRes->status)
  86. {
  87. $aResults = $oRes->result;
  88. echo "<table>\n";
  89. // Header made after the first line
  90. echo "<tr>\n";
  91. foreach ($aResults[0]->values as $aKeyValuePair)
  92. {
  93. echo " <th>".$aKeyValuePair->key."</th>\n";
  94. }
  95. echo "</tr>\n";
  96. foreach ($aResults as $iRow => $aData)
  97. {
  98. echo "<tr>\n";
  99. foreach ($aData->values as $aKeyValuePair)
  100. {
  101. echo " <td>".$aKeyValuePair->value."</td>\n";
  102. }
  103. echo "</tr>\n";
  104. }
  105. echo "</table>\n";
  106. }
  107. else
  108. {
  109. $aErrors = array();
  110. foreach ($oRes->errors->messages as $oMessage)
  111. {
  112. $aErrors[] = $oMessage->text;
  113. }
  114. $sErrorMsg = implode(', ', $aErrors);
  115. echo "<p>SearchObjects() failed with message: $sErrorMsg</p>\n";
  116. //echo "<pre>\n";
  117. //print_r($oRes);
  118. //echo "</pre>\n";
  119. }
  120. echo "</p>\n";
  121. }
  122. catch(SoapFault $e)
  123. {
  124. echo "<h1>SoapFault Exception: {$e->getMessage()}</h1>\n";
  125. echo "<h2>Request</h2>\n";
  126. echo "<pre>\n";
  127. echo htmlspecialchars($oSoapClient->__getLastRequest())."\n";
  128. echo "</pre>";
  129. echo "<h2>Response</h2>";
  130. echo $oSoapClient->__getLastResponse()."\n";
  131. }
  132. ?>