-1

I am trying to fetch search results from pubmed.

$query=(BRCA1[tiab]) OR (Breast cancer 1 gene[tiab])AND (Cancer[tiab])
$esearch = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=$query&retmax=10&usehistory=y';
$handle = fopen($esearch, "r");
$rettype = "abstract"; //retreives abstract of the record, rather than full record
$retmode = "xml";

I get this HTTP Access Failure error.

Error:

Warning: fopen(http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=(BRCA1[tiab]) OR (Breast cancer 1 gene[tiab]) AND (Cancer[tiab])&retmax=10&usehistory=y): failed to open stream: HTTP request failed! HTTP/1.1 406 Not Acceptable in /Applications/XAMPP/xamppfiles/htdocs/search.php on line 60

When I directly paste the url, http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=(BRCA1[tiab]) OR (Breast cancer 1 gene[tiab]) AND (Cancer[tiab])&retmax=10&usehistory=y I get search results in the page but not when accessing through the php script.

War10ck
  • 12,387
  • 7
  • 41
  • 54
Vignesh
  • 912
  • 6
  • 13
  • 24

1 Answers1

2

There are a few issues here. First, you have a syntax error on the first line, where you have plain text without quotes. We can fix that by replacing this line:

$query=(BRCA1[tiab]) OR (Breast cancer 1 gene[tiab])AND (Cancer[tiab]) 

with this line:

$query = "(BRCA1[tiab]) OR (Breast cancer 1 gene[tiab])AND (Cancer[tiab])";

This now fixes that syntax error.

Secondly, you have a silent string concat error in your second line. If you want to concatenate variables inline (without using the . operator) you have to use double quotes, not single quotes. Let's fix that by replacing this line:

$esearch = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=$query&retmax=10&usehistory=y'; 

with this line:

$esearch = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=$query&retmax=10&usehistory=y"; 

Lastly, you're not urlencoding the query, thus you're getting spaces in your URL that are not encoded and are messing up the URL for fopen. Let's wrap the query string in urlencode():

$query = urlencode("(BRCA1[tiab]) OR (Breast cancer 1 gene[tiab])AND (Cancer[tiab])");
$esearch = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&term=$query&retmax=10&usehistory=y"; 
$handle = fopen($esearch, "r"); 
$rettype = "abstract"; //retreives abstract of the record, rather than full record 
$retmode = "xml"; 

I tested this code on CLI and it seems to work correctly.

jsanc623
  • 534
  • 4
  • 17