From 6c89f2bc308c1a83a517b7e074c9db2f0459ae20 Mon Sep 17 00:00:00 2001 From: Sylvain Hess Date: Wed, 14 Oct 2020 19:22:34 +0200 Subject: [PATCH 1/6] change vendor name --- composer.json | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index cd60319..f42f123 100644 --- a/composer.json +++ b/composer.json @@ -1,21 +1,20 @@ { - "name": "tmpjr/pubmed", + "name": "1px/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" @@ -25,4 +24,4 @@ "PubMed": "src/" } } -} +} \ No newline at end of file From a091c1275dbb2813c7193072e8527aec0d93173c Mon Sep 17 00:00:00 2001 From: Sylvain Hess Date: Wed, 14 Oct 2020 19:22:47 +0200 Subject: [PATCH 2/6] Add api-key option --- src/PubMed/PubMed.php | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) 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); From d9efab7ab63f9a933ee95c9cd4b9a2ca98cc07af Mon Sep 17 00:00:00 2001 From: Sylvain Hess Date: Wed, 14 Oct 2020 19:23:33 +0200 Subject: [PATCH 3/6] try/catch in PubMed\Term() --- src/PubMed/Term.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/PubMed/Term.php b/src/PubMed/Term.php index 5003cac..78b1ab3 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('6b0df5dce10a4a4a74081f1e1fcea05c8d09'); $articles[] = $api->query($pmid); } From 97d22d618c12676dc78bd48b526f1292ee74dcb2 Mon Sep 17 00:00:00 2001 From: Sylvain Hess Date: Wed, 14 Oct 2020 19:25:07 +0200 Subject: [PATCH 4/6] update readme --- README.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) 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 - From 95c75e24abe6264a4e5b0681510fe3cb61665379 Mon Sep 17 00:00:00 2001 From: 1pix <62962564+1pix@users.noreply.github.com> Date: Wed, 14 Oct 2020 20:07:53 +0200 Subject: [PATCH 5/6] revert vendor name --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index f42f123..bd9772d 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "1px/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"], @@ -24,4 +24,4 @@ "PubMed": "src/" } } -} \ No newline at end of file +} From 763dbeaf68e21ee8c4296176f2a65d42f5352396 Mon Sep 17 00:00:00 2001 From: 1pix <62962564+1pix@users.noreply.github.com> Date: Wed, 14 Oct 2020 20:19:11 +0200 Subject: [PATCH 6/6] Fix setApiKey in Term->query --- src/PubMed/Term.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PubMed/Term.php b/src/PubMed/Term.php index 78b1ab3..c73e086 100755 --- a/src/PubMed/Term.php +++ b/src/PubMed/Term.php @@ -60,7 +60,7 @@ public function query($term) if ($this->articleCount > 0) { foreach ($xml->IdList->Id as $k => $pmid) { $api = new PubMedId(); - $api->setApiKey('6b0df5dce10a4a4a74081f1e1fcea05c8d09'); + $api->setApiKey($this->apiKey); $articles[] = $api->query($pmid); }