00001 <?php
00002
00009 class SDRFedSearchClient extends SObject {
00010 const RESULT_SIMPLEXML = 0;
00011 const RESULT_STRING = 1;
00012 const RESULT_OBJECTS = 2;
00013
00014 protected $lastQuery = null;
00015 protected $lastResult = null;
00016
00017 private static $XML_ERROR_TYPES = array(
00018 LIBXML_ERR_WARNING => 'Warning',
00019 LIBXML_ERR_ERROR => 'Error',
00020 LIBXML_ERR_FATAL => 'Fatal Error'
00021 );
00022
00032 public function query($req, $resultType = self::RESULT_SIMPLEXML) {
00033
00034 $this->lastQuery = clone $req;
00035
00036 $queryURL = $req->buildQueryURL();
00037 $resultRaw = utf8_encode($this->doCurlCall($queryURL));
00038
00039 $this->lastResult = $resultRaw;
00040
00041 switch($resultType) {
00042 case self::RESULT_STRING:
00043 return $resultRaw;
00044 case self::RESULT_SIMPLEXML:
00045 case self::RESULT_OBJECTS:
00046
00047 $oldErr = libxml_use_internal_errors(true);
00048 $resultXML = @simplexml_load_string($resultRaw);
00049
00050 if($resultXML == null) {
00051 $errors = libxml_get_errors();
00052 foreach($errors as $error) {
00053 $this->setError("XML " . self::$XML_ERROR_TYPES[$error->level] . " on line $error->line: $error->message");
00054 }
00055 $this->setError('Invalid XML Response.');
00056 libxml_use_internal_errors($oldErr);
00057 return false;
00058 }
00059 libxml_use_internal_errors($oldErr);
00060
00061
00062 if(isset($resultXML->error)) {
00063
00064 foreach($resultXML->error as $e)
00065 $this->setError((string)$e);
00066 return false;
00067 }
00068
00069 if($resultType == self::RESULT_SIMPLEXML)
00070 return $resultXML;
00071 $type = $req->getType();
00072 STimer::start('buildObjects');
00073 switch($type) {
00074 case SDRContract::REQUEST_TYPE_LIST:
00075 case SDRContract::REQUEST_TYPE_QUICK:
00076 case SDRContract::REQUEST_TYPE_ADVANCED:
00077 $result = new SDRResourceResponse($resultXML);
00078 break;
00079 case SDRContract::REQUEST_TYPE_VOCAB:
00080 $result = new SDRVocabResponse($resultXML);
00081 break;
00082 }
00083 STimer::end('buildObjects');
00084 return $result;
00085 }
00086
00087 $this->setError('Invalid result type specified: ' . $resultType);
00088 return false;
00089 }
00090
00101 protected function doCurlCall($url, $key = null, $XMLData = null) {
00102 STimer::start('curl');
00103 $ch = curl_init();
00104 curl_setopt($ch, CURLOPT_URL, $url);
00105 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
00106 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
00107 curl_setopt($ch, CURLOPT_TIMEOUT, 5);
00108
00109 if($key != null || $XMLData != null) {
00110 curl_setopt($ch, CURLOPT_POST, 1);
00111 curl_setopt($ch, CURLOPT_POSTFIELDS, 'key=' . $key . '&XMLData=' . $XMLData);
00112 }
00113 $store = curl_exec($ch);
00114 curl_close($ch);
00115 STimer::end('curl');
00116 return $store;
00117 }
00118
00126 public function getLastQuery() {
00127 return $this->lastQuery;
00128 }
00129
00137 public function getLastResult() {
00138 return $this->lastResult;
00139 }
00140 }
00141
00142 ?>