Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

Warning: fopen failed to open stream: HTTP request failed! HTTP/1.1 406 Not Acceptable in /XAMPP/xamppfiles/htdocs/search.php

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...