Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PubMed Commons API
<?php
if (isset($_GET['pmid'])) {
$json = file_get_contents('http://www.ncbi.nlm.nih.gov/myncbi/comments/?' . http_build_query(array(
'p$rq' => 'CommL.CommServer:com',
'cmd' => 'get',
'recid' => $_GET['pmid'],
)));
$data = json_decode($json, true);
$items = array();
if ($data['comments']) {
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($data['comments'], LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NOENT);
$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//li');
foreach ($nodes as $node) {
$items[] = array(
'id' => $node->getAttribute('data-cmid'),
'date' => $xpath->evaluate('string(//*[contains(@class, "comm_date_d")])', $node),
'text' => $xpath->evaluate('string(//*[contains(@class, "comm_content")])', $node),
'url' => 'http://www.ncbi.nlm.nih.gov' . $xpath->evaluate('string(//*[contains(@class, "comm_permalink")]/@href)', $node),
'author' => array(
'name' => $xpath->evaluate('string(//*[contains(@class, "comm_f_name")])', $node),
'comments' => 'http://www.ncbi.nlm.nih.gov' . $xpath->evaluate('string(//*[contains(@class, "comm_f_name")]/@href)', $node),
),
);
}
}
} else {
$json = file_get_contents('http://eutils.be-md.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?' . http_build_query(array(
'db' => 'pubmed',
'retmode' => 'json',
'retmax' => 100000,
'term' => 'has_user_comments[filter]'
)));
$data = json_decode($json, true);
$items = array_map(function($id) {
return 'http://www.macropus.org/2015/pubmed-commons/?pmid=' . $id;
}, $data['esearchresult']['idlist']);
}
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
print json_encode($items);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment