diff --git a/README.md b/README.md index 03f4bf8..a390c1b 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,12 @@ ### API wrapper library to interface with NCBI's PubMed Efetch Server -Getting started ---------------- +## Getting started ### Installing via Composer The recommended way to install PubMed is through [Composer](http://getcomposer.org). -1. Add ``tmpjr/pubmed`` as a dependency in your project's ``composer.json`` file: +1. Add `tmpjr/pubmed` as a dependency in your project's `composer.json` file: { "require": { @@ -15,15 +14,15 @@ The recommended way to install PubMed is through [Composer](http://getcomposer.o } } -2. Download and install Composer: +2. Download and install Composer: curl -s http://getcomposer.org/installer | php -3. Install your dependencies: +3. Install your dependencies: php composer.phar install -4. Require Composer's autoloader +4. Require Composer's autoloader Composer also prepares an autoload file that's capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code's bootstrap process: @@ -31,8 +30,7 @@ The recommended way to install PubMed is through [Composer](http://getcomposer.o You can find out more on how to install Composer, configure autoloading, and other best-practices for defining dependencies at [getcomposer.org](http://getcomposer.org). -Basic Usage ------------ +## Basic Usage ```php query(15221447); +$api->setApiKey('xxx'); print_r($article); // Search By Term $api = new PubMed\Term(); $api->setReturnMax(100); // set max returned articles, defaults to 10 +$api->setApiKey('xxx'); $articles = $api->query('CFTR'); print_r($articles); @@ -57,4 +57,3 @@ print_r($articles); Licensed under the open MIT license: http://rem.mit-license.org - diff --git a/composer.json b/composer.json index cd60319..bd9772d 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,20 @@ { - "name": "tmpjr/pubmed", + "name": " tmpjr/pubmed", "type": "library", "description": "Wrapper API for the NCBI PubMed EFetch Utilities developed at Ambry Genetics and made public for use by the community.", - "keywords": ["ncbi","pubmed","wrapper","api"], + "keywords": ["ncbi", "pubmed", "wrapper", "api"], "homepage": "https://github.com/tmpjr/pubmed", "license": "MIT", - "authors": [ - { + "authors": [{ "name": "Tom Ploskina Jr.", "email": "tploskina@ambrygen.com", "homepage": "http://ambrygen.com" }, - { + { "name": "Tom Ploskina Jr.", "email": "tploskinajr@gmail.com", "homepage": "https://github.com/tmpjr" - } + } ], "require": { "php": ">=5.3.0" diff --git a/src/PubMed/PubMed.php b/src/PubMed/PubMed.php index cd50709..c05a1f2 100755 --- a/src/PubMed/PubMed.php +++ b/src/PubMed/PubMed.php @@ -3,7 +3,7 @@ /** * PHP wrapper for NCBI PubMed * Extend Pubmed for term specific searching - * + * * @author Tom Ploskina * @copyright Copyright (c) 2013 http://tmpjr.me * @license MIT http://opensource.org/licenses/MIT @@ -36,7 +36,7 @@ abstract class PubMed * Which database from NCBI to pull from * @var string */ - protected $db = 'PubMed'; + protected $db = 'pubmed'; /** * The maximum number of articles to receive @@ -50,6 +50,12 @@ abstract class PubMed */ protected $returnStart = 0; + /** + * NCBI Api Key + * @var integer + */ + protected $apiKey = ''; + /** * NCBI URL, should be set in child class * @var string @@ -72,7 +78,7 @@ abstract class PubMed * Return mode from NCBI's API */ const RETURN_MODE = 'xml'; - + /** * Initiate the cURL connection */ @@ -86,13 +92,17 @@ public function __construct() * -- do not implement here * @return string url */ - protected function getUrl() {} + protected function getUrl() + { + } /** * Get the URI variable name, specific to child classes * @return string eg, "term" */ - protected function getSearchName() {} + protected function getSearchName() + { + } /** * Return the article count @@ -121,7 +131,16 @@ public function setReturnStart($start) return $this->returnStart = intval($start); } - /** + /** + * Set the api-key + * @param string $value the ncbi api-key + */ + public function setApiKey($key) + { + return $this->apiKey = $key; + } + + /** * Send the request to NCBI, return the raw result, * throw \Ambry\Pubmed exception on error * @param string $searchTerm What are we searching for? @@ -135,6 +154,7 @@ protected function sendRequest($searchTerm) $url .= "&retmode=" . self::RETURN_MODE; $url .= "&retstart=" . intval($this->returnStart); $url .= "&" . $this->getSearchName() . "=" . urlencode($searchTerm); + $url .= "&api_key=" . $this->apiKey; curl_setopt($this->curl, CURLOPT_URL, $url); curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->connectionTimeout); diff --git a/src/PubMed/Term.php b/src/PubMed/Term.php index 5003cac..c73e086 100755 --- a/src/PubMed/Term.php +++ b/src/PubMed/Term.php @@ -1,9 +1,9 @@ - * @copyright Copyright (c) 2013 Ambry Genetics http://www.ambrygen.com * @license MIT http://opensource.org/licenses/MIT @@ -11,6 +11,7 @@ */ namespace PubMed; + use SimpleXMLElement; class Term extends PubMed @@ -39,15 +40,27 @@ protected function getSearchName() * @return array array of New PubMed\Article objects */ public function query($term) - { + { $content = $this->sendRequest($term); - $xml = new SimpleXMLElement($content); + + libxml_use_internal_errors(true); // this turns off spitting errors on screen + try { + $xml = new SimpleXMLElement($content); + // throw new Exception('Some Error Message'); + } catch (\Exception $e) { + // var_dump($e->getMessage()); + + return; // if error end here + } + + // no error, continue $this->articleCount = (int) $xml->Count; $articles = array(); if ($this->articleCount > 0) { foreach ($xml->IdList->Id as $k => $pmid) { $api = new PubMedId(); + $api->setApiKey($this->apiKey); $articles[] = $api->query($pmid); }