问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/24019600/warning-fopen-failed-to-open-stream-http-request-failed-http-1-1-406-not-acce