. * * ***************************************************************************************************************************************************/ require_once("Variables.php"); class EntrezRequest { var $Connection; var $Type; var $CacheName; var $RequestUri; var $QueryData; var $Xml; var $Html; function EntrezRequest( $Type, $CacheName, $QueryData ) { Global $ncbi_WinFiles; $this->Type = $Type; $this->CacheName = $CacheName; $this->QueryData = $QueryData; if ($ncbi_WinFiles) { // Escape filenames when using windows $this->CacheName = str_replace("\"", "", $this->CacheName); $this->CacheName = str_replace("[", "", $this->CacheName); $this->CacheName = str_replace("]", "", $this->CacheName); } // Set up the HttpRequest Global $Connection; if (empty($Connection)) $Connection = new HttpRequest; $this->Connection = $Connection; if ($this->CheckCache()) return true; if (!$this->SendRequest()) return false; $this->SaveCache(); return true; } function CheckCache() { if (!file_exists($this->CacheName)) return false; if ((date('Y', filectime($this->CacheName)) != date('Y')) OR (date('m', filectime($this->CacheName)) != date('m'))) { // If the cached file is from last month, delete it and start again unlink($this->CacheName); return false; } $this->Xml = file_get_contents($this->CacheName); // But otherwise, we can read it into the Xml global, and exit the function return true; } function SendRequest() { Global $ncbi_DefaultTool, $ncbi_Tool, $ncbi_Email; $this->QueryData['retmode'] = "xml"; // Make sure we get XML if (!empty($ncbi_Tool)) $this->QueryData['tool'] = $ncbi_Tool; // Add tool and email to QueryData -- this is requested by ncbi to help them else $this->QueryData['tool'] = $ncbi_DefaultTool; $this->QueryData['email'] = $ncbi_Email; // deal with any problems that may arise. Set these in the "user settings" $Uris = array( "eSearch" => "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi", "eSummary" => "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi", ); $this->Connection->setUrl ( $Uris[$this->Type] ); // Now send the URL and QueryData array to the Connection object -- this uses $this->Connection->setQueryData ( $this->QueryData ); // PHP's HttpRequest extension try { $this->Connection->send(); } catch (Exception $e) { // HttpRequest is really bad at handling connection errors -- we need to catch exceptions and reload the page when these errors occur throw new Exception($e->getMessage()); return false; } $this->Xml = $this->Connection->GetResponseBody(); // Convert the response into a string, saved in the global 'Xml' // Check for errors if (strpos($this->Xml, "Unable to obtain query #1", 0)) { throw new Exception("Unable to obtain query #1."); return false; } return true; } function SaveCache() { $f = fopen($this->CacheName, 'w'); // Cache the new file fwrite($f, $this->Xml); fclose($f); if (!file_exists($this->CacheName)) $this->Html = "

Warning: could not save cache file (" . $this->CacheName . ")

"; return true; } } ?>