Beispiel #1
0
 /**
  * Make Request
  *
  * Makes a request to the Voyager Restful API
  *
  * @param array  $hierarchy Array of key-value pairs to embed in the URL path of
  * the request (set value to false to inject a non-paired value).
  * @param array  $params    A keyed array of query data
  * @param string $mode      The http request method to use (Default of GET)
  * @param string $xml       An optional XML string to send to the API
  *
  * @return obj  A Simple XML Object loaded with the xml data returned by the API
  * @access protected
  */
 protected function makeRequest($hierarchy, $params = false, $mode = "GET", $xml = false)
 {
     // Build Url Base
     $urlParams = "http://{$this->ws_host}:{$this->ws_port}/{$this->ws_app}";
     // Add Hierarchy
     foreach ($hierarchy as $key => $value) {
         $hierarchyString[] = $value !== false ? urlencode($key) . "/" . urlencode($value) : urlencode($key);
     }
     // Add Params
     foreach ($params as $key => $param) {
         $queryString[] = $key . "=" . urlencode($param);
     }
     // Build Hierarchy
     $urlParams .= "/" . implode("/", $hierarchyString);
     // Build Params
     if (isset($queryString)) {
         $urlParams .= "?" . implode("&", $queryString);
     }
     if ($mode == 'GET' && isset($this->getCache[$urlParams])) {
         return $this->getCache[$urlParams];
     }
     // Create Proxy Request
     $client = new Proxy_Request($urlParams);
     if ($this->sessionId) {
         $client->addCookie('JSESSIONID', $this->sessionId);
     }
     // Select Method
     if ($mode == "POST") {
         $client->setMethod(HTTP_REQUEST_METHOD_POST);
         if ($xml) {
             $client->addRawPostData($xml);
         }
     } else {
         if ($mode == "PUT") {
             $client->setMethod(HTTP_REQUEST_METHOD_PUT);
             $client->addRawPostData($xml);
         } else {
             if ($mode == "DELETE") {
                 $client->setMethod(HTTP_REQUEST_METHOD_DELETE);
             } else {
                 $client->setMethod(HTTP_REQUEST_METHOD_GET);
             }
         }
     }
     // Send Request and Retrieve Response
     $startTime = microtime(true);
     if (PEAR::isError($client->sendRequest())) {
         error_log("VoyagerRestful: failed to send request to {$urlParams}");
         return false;
     }
     $code = $client->getResponseCode();
     if ($code >= 400) {
         error_log("VoyagerRestful: HTTP Request failed with error code {$code}. Request url: {$urlParams}, response: " . $client->getResponseBody());
     }
     $cookies = $client->getResponseCookies();
     if ($cookies) {
         foreach ($cookies as $cookie) {
             if ($cookie['name'] == 'JSESSIONID') {
                 $this->sessionId = $cookie['value'];
             }
         }
     }
     $xmlResponse = $client->getResponseBody();
     $this->debugLog('[' . round(microtime(true) - $startTime, 4) . "s] {$this->sessionId} {$mode} request {$urlParams}, body:\n{$xml}\nResults:\n{$xmlResponse}");
     $oldLibXML = libxml_use_internal_errors();
     libxml_use_internal_errors(true);
     $simpleXML = simplexml_load_string($xmlResponse);
     libxml_use_internal_errors($oldLibXML);
     if ($simpleXML === false) {
         $logger = new Logger();
         $error = libxml_get_last_error();
         error_log('VoyagerRestful: Failed to parse response XML: ' . $error->message . ", response:\n" . $xmlResponse);
         $logger->log('Failed to parse response XML: ' . $error->message . ", response:\n" . $xmlResponse, PEAR_LOG_ERR);
         $this->debugLog('Failed to parse response XML: ' . $error->message . ", response:\n" . $xmlResponse);
         return false;
     }
     if ($mode == 'GET') {
         $this->getCache[$urlParams] = $simpleXML;
     }
     return $simpleXML;
 }