Пример #1
0
 public function browse($source, $from)
 {
     $source = strtolower($source);
     $index = $this->config[$source]['id'];
     $bases = $this->config['Global']['bases'];
     $removeRegex = $this->config[$source]['search_remove'];
     if (isset($removeRegex)) {
         $from = preg_replace($removeRegex, '', $from);
     }
     $params = array('index' => $index, 'query' => $from, 'base' => $bases);
     $answer = $this->httpService->get($this->cgiUrl, $params);
     $xml = simplexml_load_string($answer->getBody());
     $indexes = array();
     $count = 0;
     $next = null;
     foreach ($xml->{'result'} as $result) {
         $ids = array();
         foreach ($result->id as $id) {
             $ids[] = (string) $id;
         }
         $heading = $this->getDisplayText($source, (string) $result->display);
         if ($count < 10) {
             $indexes[] = array('heading' => $heading, 'ids' => $ids, 'count' => count($ids));
         } else {
             $next = (string) $result->sort;
         }
         $count++;
     }
     $result = array('items' => $indexes);
     if (isset($next)) {
         $result['nextQuery'] = array('source' => $source, 'from' => $next);
     }
     return $result;
 }
Пример #2
0
 /**
  * Perform an HTTP request.
  *
  * @param string $url    URL of request
  * @param array  $param  query parameters to add to URL
  * @param string $method HTTP method
  * @param string $body   HTTP body (null for none)
  *
  * @return SimpleXMLElement
  */
 public function doHTTPRequest($url, $params = null, $method = 'GET', $body = null, $headers = array())
 {
     if ($this->debug_enabled) {
         $fullUrl = $this->appendQueryString($url, $params);
         $this->debug("URL: '{$fullUrl}'");
     }
     if ($params == null) {
         $params = array();
     }
     $result = null;
     try {
         if ($method == 'GET') {
             $result = $this->httpService->get($url, $params, $this->timeout, $headers);
         } else {
             if ($method == 'POST') {
                 $url = $this->appendQueryString($url, $params);
                 $result = $this->httpService->post($url, $body, 'application/octet-stream', $this->timeout);
             } else {
                 $url = $this->appendQueryString($url, $params);
                 $client = $this->httpService->createClient($url, $method, $this->timeout);
                 if ($body != null) {
                     $client->setRawBody($body);
                 }
                 $result = $client->send();
             }
         }
     } catch (\Exception $e) {
         throw new ILSException($e->getMessage());
     }
     if (!$result->isSuccess()) {
         throw new ILSException('HTTP error');
     }
     $answer = $result->getBody();
     if ($this->debug_enabled) {
         $this->debug("url: {$url} response: {$answer}");
     }
     $answer = str_replace('xmlns=', 'ns=', $answer);
     $result = simplexml_load_string($answer);
     if (!$result) {
         if ($this->debug_enabled) {
             $this->debug("XML is not valid, URL: {$url}");
         }
         throw new ILSException("XML is not valid, URL: {$url} method: {$method} answer: {$answer}.");
     }
     return $result;
 }