openmeetings_rest_service.php 6.8 KB

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