/** * Make an OAI-PMH request. Throw an exception if there is an error; * return a SimpleXML object on success. * * @param string $verb OAI-PMH verb to execute. * @param array $params GET parameters for ListRecords method. * * @return DOMDocument Response as DOM * @throws Exception * @throws HTTP_Request2_LogicException */ protected function sendRequest($verb, $params = []) { // Set up the request: $request = new HTTP_Request2($this->baseURL, HTTP_Request2::METHOD_GET, ['ssl_verify_peer' => false]); $request->setHeader('User-Agent', 'RecordManager'); // Load request parameters: $url = $request->getURL(); $params['verb'] = $verb; $url->setQueryVariables($params); $urlStr = $url->getURL(); if ($this->debugLog) { file_put_contents($this->debugLog, date('Y-m-d H:i:s') . ' [' . getmypid() . "] Request:\n{$urlStr}\n", FILE_APPEND); } // Perform request and throw an exception on error: for ($try = 1; $try <= $this->maxTries; $try++) { $this->message("Sending request: {$urlStr}", true); try { $response = $request->send(); $code = $response->getStatus(); if ($code >= 300) { if ($try < $this->maxTries) { $this->message("Request '{$urlStr}' failed ({$code}), retrying in " . "{$this->retryWait} seconds...", false, Logger::WARNING); sleep($this->retryWait); continue; } $this->message("Request '{$urlStr}' failed: {$code}", false, Logger::FATAL); throw new Exception("{$this->source}: Request failed: {$code}"); } $responseStr = $response->getBody(); if ($this->debugLog) { file_put_contents($this->debugLog, date('Y-m-d H:i:s') . ' [' . getmypid() . "] Response:\n" . "{$responseStr}\n\n", FILE_APPEND); } return $this->processResponse($responseStr, isset($params['resumptionToken'])); } catch (Exception $e) { if ($try < $this->maxTries) { $this->message("Request '{$urlStr}' failed (" . $e->getMessage() . "), retrying in {$this->retryWait} seconds...", false, Logger::WARNING); sleep($this->retryWait); continue; } throw new Exception("{$this->source}: " . $e->getMessage()); } } throw new Exception("{$this->source}: Request failed"); }
/** * Call MetaLib X-Server * * @param array $params URL Parameters * * @return string XML * @throws Exception */ protected function callXServer($params) { $request = new HTTP_Request2($this->baseURL, HTTP_Request2::METHOD_GET, ['ssl_verify_peer' => false]); $request->setHeader('User-Agent', 'RecordManager'); $url = $request->getURL(); $url->setQueryVariables($params); $cleanUrl = preg_replace('/user_password=([^&]+)/', 'user_password=***', $url->getURL()); $this->_message("Sending request: {$cleanUrl}", true); $response = $request->send(); $code = $response->getStatus(); if ($code >= 300) { $this->_message("Request '{$url}' failed: {$code}", false, Logger::FATAL); throw new Exception("Request failed: {$code}"); } $this->_message("Request successful", true); return $response->getBody(); }
/** * Fetch a file to be harvested * * @param string $filename File to retrieve * * @return string xml * @throws Exception */ protected function retrieveFile($filename) { $request = new HTTP_Request2($this->baseURL . $filename, HTTP_Request2::METHOD_GET, ['ssl_verify_peer' => false]); $request->setHeader('User-Agent', 'RecordManager'); $url = $request->getURL(); $urlStr = $url->getURL(); $this->message("Sending request: {$urlStr}", true); // Perform request and throw an exception on error: $response = null; for ($try = 1; $try <= 5; $try++) { try { $response = $request->send(); } catch (Exception $e) { if ($try < 5) { $this->message("Request '{$urlStr}' failed (" . $e->getMessage() . "), retrying in 30 seconds...", false, Logger::WARNING); sleep(30); continue; } throw $e; } if ($try < 5) { $code = $response->getStatus(); if ($code >= 300) { $this->message("Request '{$urlStr}' failed ({$code}), retrying in " . "30 seconds...", false, Logger::WARNING); sleep(30); continue; } } break; } $code = is_null($response) ? 999 : $response->getStatus(); if ($code >= 300) { $this->message("Request '{$urlStr}' failed: {$code}", false, Logger::FATAL); throw new Exception("Request failed: {$code}"); } return $response->getBody(); }