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; }
/** * Send an NCIP request. * * @param string $xml * XML request document * * @return object SimpleXMLElement parsed from response */ protected function sendRequest($xml) { // Make the NCIP request: try { $client = $this->httpService->createClient($this->config['Catalog']['url']); $client->setRawBody($xml); $client->setEncType('application/xml; "charset=utf-8"'); $client->setMethod('POST'); if (isset($this->timeout)) { $client->setOptions(array('timeout' => $this->timeout)); } if (isset($this->config['Catalog']['username']) && isset($this->config['Catalog']['password'])) { $user = $this->config['Catalog']['username']; $password = $this->config['Catalog']['password']; $client->setAuth($user, $password); } if ($this->hasUntrustedSSL) { // Do not verify SSL certificate $client->setOptions(array('adapter' => 'Zend\\Http\\Client\\Adapter\\Curl', 'curloptions' => array(CURLOPT_FOLLOWLOCATION => true, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false))); } elseif (isset($this->cacert)) { $client->setOptions(array('adapter' => 'Zend\\Http\\Client\\Adapter\\Curl', 'curloptions' => array(CURLOPT_FOLLOWLOCATION => true, CURLOPT_CAINFO => $this->cacert))); } $result = $client->send(); } catch (\Exception $e) { $message = $e->getMessage(); $this->addEnviromentalException($message); throw new ILSException($message); } if (!$result->isSuccess()) { $message = 'HTTP error: ' . $result->getContent(); $this->addEnviromentalException($message); throw new ILSException($message); } // Process the NCIP response: $body = $result->getBody(); $response = @simplexml_load_string($body); if (!is_a($response, 'SimpleXMLElement')) { $message = "Problem parsing XML"; $this->addEnviromentalException($message); throw new ILSException($message); } $response->registerXPathNamespace('ns1', 'http://www.niso.org/2008/ncip'); if ($problem = $this->getProblem($response)) { // TODO chcek problem type $message = 'Problem recieved in XCNCIP2 Driver .. Content:' . str_replace('\\n', '<br/>', $problem); $this->addEnviromentalException($message); throw new ILSException($message); } return $response; }
public function testGetMyProfileUsingRestDLF() { $this->setUpDriver('Aleph_XServer_disabled.ini'); $patronId = 'TEST'; $user = array('id' => 'TEST'); $patronAddressUrl = self::DLF_API_BASE_URL . "/patron/{$patronId}/patronInformation/address/"; $patronAddressResponse = $this->getResponse('v20.2.10', 'patronInformationAddressResponse.xml'); $this->mockedHttpService->expects($this->at(0))->method('get')->with($this->equalTo($patronAddressUrl), $this->equalTo(array()), $this->equalTo(null))->will($this->returnValue($patronAddressResponse)); $patronRegistrationUrl = self::DLF_API_BASE_URL . "/patron/{$patronId}/patronStatus/registration/"; $patronRegistrationResponse = $this->getResponse('v20.2.10', 'patronStatusRegistration.xml'); $this->mockedHttpService->expects($this->at(1))->method('get')->with($this->equalTo($patronRegistrationUrl), $this->equalTo(array()), $this->equalTo(null))->will($this->returnValue($patronRegistrationResponse)); $expectedResult = array('lastname' => 'John Smith', 'firstname' => '', 'address1' => 'Street 123', 'address2' => '12345 Springfield', 'barcode' => 'TEST', 'zip' => '', 'phone' => '+042 123 456 789', 'email' => '*****@*****.**', 'addressValidFrom' => '08-30-2010', 'addressValidTo' => '12-31-2013', 'id' => 'TEST', 'expire' => '08-08-2014', 'group' => '04 - S'); $realResult = $this->driver->getMyProfile($user); $this->assertEquals($expectedResult, $realResult); }
/** * Make Ilsdi Request Array * * Makes a request to the Koha ILSDI API * * @param string $service Called function (GetAvailability, * GetRecords, * GetAuthorityRecords, * LookupPatron, * AuthenticatePatron, * GetPatronInfo, * GetPatronStatus, * GetServices, * RenewLoan, * HoldTitle, * HoldItem, * CancelHold) * @param array $params Key is parameter name, value is parameter value * @param string $http_method HTTP method (default = GET) * * @throws ILSException * @return obj */ protected function makeIlsdiRequest($service, $params, $http_method = "GET") { $start = microtime(true); $url = $this->ilsBaseUrl . "?service=" . $service; foreach ($params as $paramname => $paramvalue) { $url .= "&{$paramname}=" . urlencode($paramvalue); } $this->debug("URL: '{$url}'"); $http_headers = ["Accept: text/xml", "Accept-encoding: plain"]; try { $client = $this->httpService->createClient($url); $client->setMethod($http_method); $client->setHeaders($http_headers); $result = $client->send(); } catch (\Exception $e) { $this->debug("Result is invalid."); throw new ILSException($e->getMessage()); } if (!$result->isSuccess()) { $this->debug("Result is invalid."); throw new ILSException('HTTP error'); } $end = microtime(true); $time1 = $end - $start; $start = microtime(true); $result = simplexml_load_string($result->getBody()); if (!$result) { $this->debug("XML is not valid, URL: {$url}"); throw new ILSException("XML is not valid, URL: {$url}"); } $end = microtime(true); $time2 = $end - $start; echo "\t{$time1} - {$time2}"; return $result; }
/** * 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; }