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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…