soap.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. require_once __DIR__.'/webservice.php';
  8. /**
  9. * SOAP error handler. Handles an error sending a SOAP fault.
  10. */
  11. class WSSoapErrorHandler implements WSErrorHandler
  12. {
  13. /**
  14. * Handles the error by sending a SOAP fault through the server.
  15. *
  16. * @param WSError Error to handle
  17. */
  18. public function handle($error)
  19. {
  20. $server = WSSoapServer::singleton();
  21. $server->fault(strval($error->code), $error->message);
  22. }
  23. }
  24. /**
  25. * SOAP server wrapper implementing a Singleton.
  26. */
  27. class WSSoapServer
  28. {
  29. /**
  30. * SOAP server instance.
  31. *
  32. * @var soap_server
  33. */
  34. private static $_instance;
  35. /**
  36. * Constructor.
  37. */
  38. public function __construct()
  39. {
  40. }
  41. /**
  42. * Singleton method.
  43. */
  44. public static function singleton()
  45. {
  46. if (!isset(self::$_instance)) {
  47. self::$_instance = new soap_server();
  48. // Set the error handler
  49. WSError::setErrorHandler(new WSSoapErrorHandler());
  50. // Configure the service
  51. self::$_instance->configureWSDL('WSService', 'urn:WSService');
  52. }
  53. return self::$_instance;
  54. }
  55. }
  56. $s = WSSoapServer::singleton();
  57. $s->wsdl->addComplexType(
  58. 'result',
  59. 'complexType',
  60. 'struct',
  61. 'all',
  62. '',
  63. [
  64. 'code' => ['name' => 'code', 'type' => 'xsd:int'],
  65. 'message' => ['name' => 'message', 'type' => 'xsd:string'],
  66. ]
  67. );
  68. $s->wsdl->addComplexType(
  69. 'extras',
  70. 'complexType',
  71. 'struct',
  72. 'all',
  73. '',
  74. [
  75. 'field_name' => ['name' => 'field_name', 'type' => 'xsd:string'],
  76. 'field_value' => ['name' => 'field_value', 'type' => 'xsd:string'],
  77. ]
  78. );
  79. $s->wsdl->addComplexType(
  80. 'extra_field',
  81. 'complexType',
  82. 'array',
  83. '',
  84. 'SOAP-ENC:Array',
  85. [],
  86. [
  87. [
  88. 'ref' => 'SOAP-ENC:arrayType',
  89. 'wsdl:arrayType' => 'tns:extras[]',
  90. ],
  91. ],
  92. 'tns:extras'
  93. );
  94. /*
  95. $s->wsdl->addComplexType(
  96. 'extra_field',
  97. 'complexType',
  98. 'struct',
  99. 'all',
  100. '',
  101. array(
  102. 'field_name' => array('name' => 'field_name', 'type' => 'xsd:string'),
  103. 'field_value' => array('name' => 'field_value', 'type' => 'xsd:string')
  104. )
  105. );
  106. */
  107. $s->register(
  108. 'WS.test',
  109. [],
  110. ['return' => 'xsd:string']
  111. );
  112. require_once __DIR__.'/soap_user.php';
  113. require_once __DIR__.'/soap_course.php';
  114. require_once __DIR__.'/soap_session.php';
  115. require_once __DIR__.'/soap_report.php';
  116. // Use the request to (try to) invoke the service
  117. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  118. $s->service($HTTP_RAW_POST_DATA);