soap.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. require_once '../inc/global.inc.php';
  3. require_once(dirname(__FILE__).'/webservice.php');
  4. $libpath = api_get_path(LIBRARY_PATH);
  5. require_once $libpath.'nusoap/nusoap.php';
  6. /**
  7. * SOAP error handler. Handles an error sending a SOAP fault
  8. */
  9. class WSSoapErrorHandler implements WSErrorHandler {
  10. /**
  11. * Handles the error by sending a SOAP fault through the server
  12. *
  13. * @param WSError Error to handle
  14. */
  15. public function handle($error) {
  16. $server = WSSoapServer::singleton();
  17. $server->fault(strval($error->code), $error->message);
  18. }
  19. }
  20. /**
  21. * SOAP server wrapper implementing a Singleton
  22. */
  23. class WSSoapServer {
  24. /**
  25. * SOAP server instance
  26. *
  27. * @var soap_server
  28. */
  29. private static $_instance;
  30. /**
  31. * Private constructor
  32. */
  33. private function __construct() {
  34. }
  35. /**
  36. * Singleton method
  37. */
  38. public static function singleton() {
  39. if(!isset(self::$_instance)) {
  40. self::$_instance = new soap_server();
  41. // Set the error handler
  42. WSError::setErrorHandler(new WSSoapErrorHandler());
  43. // Configure the service
  44. self::$_instance->configureWSDL('WSService', 'urn:WSService');
  45. }
  46. return self::$_instance;
  47. }
  48. }
  49. $s = WSSoapServer::singleton();
  50. $s->wsdl->addComplexType(
  51. 'result',
  52. 'complexType',
  53. 'struct',
  54. 'all',
  55. '',
  56. array(
  57. 'code' => array('name' => 'code', 'type' => 'xsd:int'),
  58. 'message' => array('name' => 'message', 'type' => 'xsd:string')
  59. )
  60. );
  61. $s->wsdl->addComplexType(
  62. 'extra_field',
  63. 'complexType',
  64. 'struct',
  65. 'all',
  66. '',
  67. array(
  68. 'field_name' => array('name' => 'field_name', 'type' => 'xsd:string'),
  69. 'field_value' => array('name' => 'field_value', 'type' => 'xsd:string')
  70. )
  71. );
  72. $s->register(
  73. 'WS.test',
  74. array(),
  75. array('return' => 'xsd:string')
  76. );
  77. require_once(dirname(__FILE__).'/soap_user.php');
  78. require_once(dirname(__FILE__).'/soap_course.php');
  79. require_once(dirname(__FILE__).'/soap_session.php');
  80. // Use the request to (try to) invoke the service
  81. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
  82. $s->service($HTTP_RAW_POST_DATA);