openmeetings_rest_service.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License") + you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. /**
  21. * Created on 03.01.2012 by eugen.schwert@gmail.com.
  22. *
  23. * @package chamilo.plugin.openmeetings
  24. * @requires CURL
  25. */
  26. /**
  27. * Class OpenMeetingsRestService.
  28. */
  29. class OpenMeetingsRestService
  30. {
  31. public function call($request, $returnAttribute = "return")
  32. {
  33. // This will allow you to view errors in the browser
  34. // Note: set "display_errors" to 0 in production
  35. // ini_set('display_errors',1);
  36. // Report all PHP errors (notices, errors, warnings, etc.)
  37. // error_reporting(E_ALL);
  38. // URI used for making REST call. Each Web Service uses a unique URL.
  39. // $request
  40. // Initialize the session by passing the request as a parameter
  41. $session = curl_init($request);
  42. // Set curl options by passing session and flags
  43. // CURLOPT_HEADER allows us to receive the HTTP header
  44. curl_setopt($session, CURLOPT_HEADER, true);
  45. // CURLOPT_RETURNTRANSFER will return the response
  46. curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
  47. // Make the request
  48. $response = curl_exec($session);
  49. // Close the curl session
  50. curl_close($session);
  51. // Confirm that the request was transmitted to the OpenMeetings! Image Search Service
  52. if (!$response) {
  53. die("Request OpenMeetings! OpenMeetings Service failed and no response was returned in ".__CLASS__.'::'.__FUNCTION__.'()');
  54. }
  55. // Create an array to store the HTTP response codes
  56. $status_code = [];
  57. // Use regular expressions to extract the code from the header
  58. preg_match('/\d\d\d/', $response, $status_code);
  59. $bt = debug_backtrace();
  60. $caller = array_shift($bt);
  61. $extra = ' (from '.$caller['file'].' at line '.$caller['line'].') ';
  62. // Check the HTTP Response code and display message if status code is not 200 (OK)
  63. switch ($status_code[0]) {
  64. case 200:
  65. // Success
  66. break;
  67. case 503:
  68. error_log('Your call to OpenMeetings Web Services '.$extra.' failed and returned an HTTP status of 503.
  69. That means: Service unavailable. An internal problem prevented us from returning data to you.');
  70. return false;
  71. break;
  72. case 403:
  73. error_log('Your call to OpenMeetings Web Services '.$extra.' failed and returned an HTTP status of 403.
  74. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.');
  75. return false;
  76. break;
  77. case 400:
  78. // You may want to fall through here and read the specific XML error
  79. error_log('Your call to OpenMeetings Web Services '.$extra.' failed and returned an HTTP status of 400.
  80. That means: Bad request. The parameters passed to the service did not match as expected.
  81. The exact error is returned in the XML response.');
  82. return false;
  83. break;
  84. default:
  85. error_log('Your call to OpenMeetings Web Services '.$extra.' returned an unexpected HTTP status of: '.$status_code[0]." Request ".$request);
  86. return false;
  87. }
  88. // Get the XML from the response, bypassing the header
  89. if (!($xml = strstr($response, '<ns'))) {
  90. $xml = null;
  91. }
  92. $dom = new DOMDocument();
  93. $dom->loadXML($xml);
  94. if ($returnAttribute == "") {
  95. //echo "XML".$xml."<br/>";
  96. return $this->getArray($dom);
  97. } else {
  98. $returnNodeList = $dom->getElementsByTagName($returnAttribute);
  99. $ret = [];
  100. foreach ($returnNodeList as $returnNode) {
  101. if ($returnNodeList->length == 1) {
  102. return $this->getArray($returnNode);
  103. } else {
  104. $ret[] = $this->getArray($returnNode);
  105. }
  106. }
  107. return $ret;
  108. }
  109. }
  110. public function getArray($node)
  111. {
  112. if (is_null($node) || !is_object($node)) {
  113. return $node;
  114. }
  115. $array = false;
  116. /*
  117. echo("!!!!!!!! NODE " . XML_TEXT_NODE
  118. . " :: name = " . $node->nodeName
  119. . " :: local = " . $node->localName
  120. . " :: childs ? " . $node->hasChildNodes()
  121. . " :: count = " . ($node->hasChildNodes() ? $node->childNodes->length : -1)
  122. . " :: type = " . $node->nodeType
  123. . " :: val = " . $node->nodeValue
  124. . "\n");
  125. /*
  126. if ($node->hasAttributes()) {
  127. foreach ($node->attributes as $attr) {
  128. $array[$attr->nodeName] = $attr->nodeValue;
  129. }
  130. }
  131. */
  132. if ($node->hasChildNodes()) {
  133. foreach ($node->childNodes as $childNode) {
  134. if ($childNode->nodeType != XML_TEXT_NODE) {
  135. if ($node->hasAttributes()) {
  136. foreach ($node->attributes as $attr) {
  137. if ($attr->localName == "nil") {
  138. return null;
  139. }
  140. }
  141. }
  142. if ($childNode->childNodes->length == 1) {
  143. $array[$childNode->localName] = $this->getArray($childNode);
  144. } else {
  145. $array[$childNode->localName][] = $this->getArray($childNode);
  146. }
  147. } else {
  148. return $childNode->nodeValue;
  149. //echo("!!!!!!!! TEXT " . $childNode->nodeValue . "\n");
  150. //$array[$childNode->localName]
  151. }
  152. }
  153. }
  154. return $array;
  155. }
  156. public function getError()
  157. {
  158. return false;
  159. }
  160. public function fault()
  161. {
  162. return false;
  163. }
  164. }