<?php
// Pubmed object
/**
* @file pubmed.php
*
*/
require_once("Net/HTTP/Client.php");
//require_once("XPath.class.php");
/**
* @brief Pubmed object
*
* Encapsulates access to a Pubmed reference.
*
*/
class Pubmed {
var $pmid;
var $data;
//----------------------------------------------------------------------------
function Pubmed ($id)
{
$this->pmid = $id;
$this->data = "";
}
//----------------------------------------------------------------------------
/**
* @brief Get Pubmed record in XML.
*
* If record has not yet been obtained we call Fetch to retrieve it.
*
*/
function Get ()
{
if ($this->data == "")
{
$this->Fetch();
}
return $this->data;
}
//----------------------------------------------------------------------------
/**
*
* Fetch Pubmed XML from a remote store
*/
function Fetch ()
{
global $config;
// Connect to Pubmed database
$http = new Net_HTTP_Client();
if ($config['proxy_name'] != "")
{
$http->setProxy ($config['proxy_name'], $config['proxy_port']);
}
$http->Connect( $config['ncbi_url'], 80 )
or die( "Connect problem" );
// Construct query NCBI for Pubmed
$query = $config['eutils'];
$query .= "&retmode=xml&db=pubmed&id=";
$query .= $this->pmid;
//echo $query;
// Go!
$status = $http->Get( $query );
if( $status != 200 )
die( "Problem : " . $http->getStatusMessage() );
else
{
$this->data = $http->getBody();
}
//echo $this->data;
$http->Disconnect();
/*
$xmlfilename = "11839199.xml";
$myfile = @fopen($xmlfilename, "r") or die("could't open file \"$xmlfilename\"");
$this->data = @fread($myfile, filesize ($xmlfilename));
fclose($myfile);
*/
}
//----------------------------------------------------------------------------
/**
* @brief Get abstract from Pubmed record
*
* Use XPath query to pull out abstract
*
*/
function GetAbstract ()
{
$abstract == "";
$this->Get();
if (PHP_VERSION >= 5.0)
{
$dom= new DOMDocument;
$dom->loadXML($this->data);
$xpath = new DOMXPath($dom);
$xpath_query = "//Abstract/AbstractText";
$nodeCollection = $xpath->query ($xpath_query);
foreach($nodeCollection as $node)
{
$abstract = $node->firstChild->nodeValue;
}
}
else
{
$xpath = new XPath();
$xpath->importFromString ($this->data);
$nodeCollection = $xpath->match("//Abstract/AbstractText");
if (!empty($nodeCollection))
{
// Get abstract
foreach($nodeCollection as $node)
{
$abstract .= $xpath->getData($node);
}
}
}
return $abstract;
}
}
?>