Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
### 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": {
"tmpjr/pubmed": "dev-master"
}
}

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:

require 'vendor/autoload.php';

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
<?php
Expand All @@ -42,11 +40,13 @@ require 'vendor/autoload.php';
// Search By PMID
$api = new PubMed\PubMedId();
$article = $api->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);

Expand All @@ -57,4 +57,3 @@ print_r($articles);
Licensed under the open MIT license:

http://rem.mit-license.org

11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
32 changes: 26 additions & 6 deletions src/PubMed/PubMed.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* PHP wrapper for NCBI PubMed
* Extend Pubmed for term specific searching
*
*
* @author Tom Ploskina <tploskinajr@gmail.com>
* @copyright Copyright (c) 2013 http://tmpjr.me
* @license MIT http://opensource.org/licenses/MIT
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -72,7 +78,7 @@ abstract class PubMed
* Return mode from NCBI's API
*/
const RETURN_MODE = 'xml';

/**
* Initiate the cURL connection
*/
Expand All @@ -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
Expand Down Expand Up @@ -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?
Expand All @@ -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);
Expand Down
21 changes: 17 additions & 4 deletions src/PubMed/Term.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php
<?php

/**
* PHP wrapper for NCBI PubMed
* Extend Pubmed for term specific searching
*
*
* @author Tom Ploskina <tploskina@ambrygen.com>
* @copyright Copyright (c) 2013 Ambry Genetics http://www.ambrygen.com
* @license MIT http://opensource.org/licenses/MIT
* @version 1.0
*/

namespace PubMed;

use SimpleXMLElement;

class Term extends PubMed
Expand Down Expand Up @@ -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);
}

Expand Down