WebServiceQuerier.php
Go to the documentation of this file.
00001 <?php 00002 00003 // $Id$ 00004 00007 00027 class WebServiceQuerier 00028 { 00030 private $url; 00031 00033 private $method; 00034 00036 private $parameters; 00037 00039 private $mime; 00040 00042 private $queryStatus = ""; // The HTTP query status code returned by the server (ex: 200) 00043 00045 private $queryStatusMessage = ""; // The HTTP query status message returned by the server (ex: OK) 00046 00048 private $queryStatusMessageDescription = 00049 ""; // The HTTP query status message description returned by the server (ex: No subject concept) 00050 00052 private $queryResultset = ""; 00053 00055 private $errorId = ""; 00056 00058 private $errorName = ""; 00059 00061 private $errorDescription = ""; 00062 00064 private $errorDebugInfo = ""; 00065 00069 public $error; 00070 00087 function __construct($url, $method, $mime, $parameters, $timeout = 0) 00088 { 00089 $this->url = $url; 00090 $this->method = $method; 00091 $this->parameters = $parameters; 00092 $this->mime = $mime; 00093 $this->timeout = $timeout; 00094 00095 $this->queryWebService(); 00096 } 00097 00098 function __destruct(){} 00099 00108 function queryWebService() 00109 { 00110 $ch = curl_init(); 00111 00112 switch ($this->method) 00113 { 00114 case "get": 00115 curl_setopt($ch, CURLOPT_URL, $this->url . "?" . $this->parameters); 00116 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: $this->mime")); 00117 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 00118 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 00119 00120 if ($this->timeout > 0) 00121 { 00122 curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); 00123 } 00124 curl_setopt($ch, CURLOPT_HEADER, TRUE); 00125 break; 00126 00127 case "post": 00128 // Check if the size of the converted file is bigger than the maximum file size 00129 // we can upload via Curl. 00130 $uploadMaxFileSize = ini_get("post_max_size"); 00131 $uploadMaxFileSize = str_replace("M", "000000", $uploadMaxFileSize); 00132 00133 if($uploadMaxFileSize > strlen($this->parameters)) 00134 { 00135 curl_setopt($ch, CURLOPT_URL, $this->url); 00136 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: $this->mime")); 00137 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 00138 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 00139 curl_setopt($ch, CURLOPT_POST, 1); 00140 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->parameters); 00141 00142 if ($this->timeout > 0) 00143 { 00144 curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); 00145 } 00146 curl_setopt($ch, CURLOPT_HEADER, TRUE); 00147 } 00148 else 00149 { 00150 $this->error = new QuerierError("WSF-600", "Fatal", $ws, "Query too big", 00151 "The query sent to the structWSF endpoint is too big given 00152 the current settings of the instance. The size of the 00153 query is ".number_format(strlen($this->parameters), 0, " ", " ")." bytes, 00154 and the autorized size of the query is ".$uploadMaxFileSize." bytes", $data); 00155 00156 return(FALSE); 00157 } 00158 break; 00159 00160 default: 00161 return FALSE; 00162 break; 00163 } 00164 00165 $xml_data = curl_exec($ch); 00166 00167 if ($xml_data === FALSE) 00168 { 00169 $data = 00170 substr($xml_data, strpos($xml_data, "\r\n\r\n") + 4, strlen($xml_data) - (strpos($xml_data, "\r\n\r\n") - 4)); 00171 00172 // Can't reach the remote server 00173 $this->queryStatus = "503"; 00174 $this->queryStatusMessage = "Service Unavailable"; 00175 $this->queryStatusMessageDescription = "Can't reach remote server (" . curl_error($ch) . ")"; 00176 $this->queryResultset = $data; 00177 00178 $this->error = new QuerierError("HTTP-500", "Warning", $this->url, "Can't reach remote server", 00179 "Can't reach remote server (" . curl_error($ch) . ")", $data); 00180 00181 return; 00182 } 00183 00184 // Remove any possible "HTTP/1.1 100 Continue" message from the web server 00185 $xml_data = str_replace("HTTP/1.1 100 Continue\r\n\r\n", "", $xml_data); 00186 00187 $header = substr($xml_data, 0, strpos($xml_data, "\r\n\r\n")); 00188 00189 $data = 00190 substr($xml_data, strpos($xml_data, "\r\n\r\n") + 4, strlen($xml_data) - (strpos($xml_data, "\r\n\r\n") - 4)); 00191 00192 curl_close($ch); 00193 00194 // check returned message 00195 $this->queryStatus = substr($header, 9, 3); 00196 $this->queryStatusMessage = substr($header, 13, strpos($header, "\r\n") - 13); 00197 00198 // Make sure that we don't have a PHP Parsing error in the resultset. If it is the case, we have to return an error 00199 if (stripos($data, "<b>Parse error</b>") !== false) 00200 { 00201 $this->queryStatus = "500"; 00202 $this->queryStatusMessage = "Internal Server Error"; 00203 $this->queryStatusMessageDescription = "Parsing Error"; 00204 $this->queryResultset = $data; 00205 00206 preg_match("/\/ws\/(.*)\/.*\.php/Uim", $data, $ws); 00207 00208 $ws = $ws[0]; 00209 $ws = substr($ws, 0, strrpos($ws, "/") + 1); 00210 00211 $this->error = new QuerierError("HTTP-500", "Fatal", $ws, "Parsing Error", "PHP Parsing Error", $data); 00212 00213 return; 00214 } 00215 00216 // Make sure that we don't have any rogue PHP uncatched fatal error in the resultset. If it is the case, 00217 // we have to return an error 00218 if (stripos($data, "<b>Fatal error</b>") !== false) 00219 { 00220 $this->queryStatus = "500"; 00221 $this->queryStatusMessage = "Internal Server Error"; 00222 $this->queryStatusMessageDescription = "Fatal Error"; 00223 $this->queryResultset = $data; 00224 00225 preg_match("/\/ws\/(.*)\/.*\.php/Uim", $data, $ws); 00226 00227 $ws = $ws[0]; 00228 $ws = substr($ws, 0, strrpos($ws, "/") + 1); 00229 00230 $this->error = new QuerierError("HTTP-500", "Fatal", $ws, "Fatal Error", "PHP uncatched Fatal Error", $data); 00231 } 00232 00233 // We have to continue. Let fix this to 200 OK so that this never raise errors within the WSF 00234 if ($this->queryStatus == "100") 00235 { 00236 $this->queryStatus = "200"; 00237 $this->queryStatusMessage = "OK"; 00238 $this->queryStatusMessageDescription = ""; 00239 $this->queryResultset = $data; 00240 return; 00241 } 00242 00243 if ($this->queryStatus != "200") 00244 { 00245 $this->queryStatusMessageDescription = str_replace(array( 00246 "\r", 00247 "\n" 00248 ), "", $data); 00249 00250 // XML error messages 00251 if (strpos($this->queryStatusMessageDescription, "<error>")) 00252 { 00253 preg_match("/.*<id>(.*)<\/id>.*/Uim", $this->queryStatusMessageDescription, $errorId); 00254 $errorId = $errorId[1]; 00255 00256 preg_match("/.*<level>(.*)<\/level>.*/Uim", $this->queryStatusMessageDescription, $errorLevel); 00257 $errorLevel = $errorLevel[1]; 00258 00259 preg_match("/.*<webservice>(.*)<\/webservice>.*/Uim", $this->queryStatusMessageDescription, $errorWS); 00260 $errorWS = $errorWS[1]; 00261 00262 preg_match("/.*<name>(.*)<\/name>.*/Uim", $this->queryStatusMessageDescription, $errorName); 00263 $errorName = $errorName[1]; 00264 00265 preg_match("/.*<description>(.*)<\/description>.*/Uim", $this->queryStatusMessageDescription, 00266 $errorDescription); 00267 $errorDescription = $errorDescription[1]; 00268 00269 preg_match("/.*<debugInformation>(.*)<\/debugInformation>.*/Uim", $this->queryStatusMessageDescription, 00270 $errorDebugInfo); 00271 $errorDebugInfo = $errorDebugInfo[1]; 00272 00273 $this->error = new QuerierError($errorId, $errorLevel, $errorWS, $errorName, $errorDescription, $errorDebugInfo); 00274 00275 return; 00276 } 00277 00278 // JSON error messages 00279 } 00280 else 00281 { 00282 $this->queryResultset = $data; 00283 } 00284 00285 return; 00286 } 00287 00298 public function getStatus() 00299 { 00300 return $this->queryStatus; 00301 } 00302 00313 public function getStatusMessage() 00314 { 00315 return $this->queryStatusMessage; 00316 } 00317 00328 public function getStatusMessageDescription() 00329 { 00330 return $this->queryStatusMessageDescription; 00331 } 00332 00343 public function getResultset() 00344 { 00345 return $this->queryResultset; 00346 } 00347 } 00348 00350 00351 00360 class QuerierError 00361 { 00363 public $id = 0; 00364 00366 public $level = 0; 00367 00369 public $webservice = ""; 00370 00372 public $name = ""; 00373 00375 public $description = ""; 00376 00378 public $debugInfo = ""; 00379 00397 function __construct($id, $level, $webservice, $name, $description, $debugInfo) 00398 { 00399 $this->id = $id; 00400 $this->level = strtolower($level); 00401 $this->webservice = $webservice; 00402 $this->name = $name; 00403 $this->description = $description; 00404 $this->debugInfo = $debugInfo; 00405 } 00406 00407 00408 function __destruct(){} 00409 } 00410 00411 ?>
