<p>I don't know PHP, so I'm sure there are better ways of doing this, but you had various issues in your code. This seems to be a working version of what you're after:</p>

<pre class="lang-php prettyprint-override"><code>&lt;?php
$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils"; $db = "gene"; $gene_symbols = array("A20","HBB","ABO");

$species = array("homo%20sapiens", "Escherichia%20coli","Saccharomyces%20cerevisiae");
$links = array();

foreach ($gene_symbols as $key =&gt; $GS){ 
  $SP = $species[$key];
  $query = "$GS%20AND%20$SP%20[orgn]%20AND%20alive";

  $esearch = "$eutils/esearch.fcgi?db=$db&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=";

  echo "$esearch$query\n";
  $links[] = file_get_contents($esearch.$query);
  foreach($links as $lk){
    echo "|------ $lk ----|";
    $xml = simplexml_load_string($lk) or die("feed not loading");
    var_dump($xml);
  }
}
?&gt;
</code></pre>

<p>There doesn't seem to be a <code>get()</code> function in PHP, you want <code>file_get_contents</code>. Also, your loops were nested incorrectly and you needed <code>var_dump($xml)</code> to dump the variable <code>$xml</code> not <code>var_dump(xml);</code> which tries to dump the string <code>xml</code>. <a href="https://stackoverflow.com/a/21661663/1081936">You're not getting a file here but a string</a>, so you need <code>simplexml_load_string</code>. Finally, you can't have unescaped whitespace in URLs. </p>

<p>Next time, the way to debug this sort of thing is to have it print the URL it is generating and try to visit it manually. You would have seen the spaces and noticed that the working example you show uses <code>%20</code> instead of space.</p>

CC BY-SA 3.0
2018-04-27T20:07:29.837Z
2018-04-27T20:07:29.837Z
terdon
<pre><code>$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";

   $databank = "gene";
   $gene_symbols = array("A20","HBB","ABO");

   $species = array("homo sapiens", "Escherichia coli","Saccharomyces cerevisiae");

foreach($gene_symbols as $value =&gt; $GS){
    $Species = $species[$value];


    $esearch = "$GS%AND%alive[prop]%AND+$Species[orgn]";
    $esearch_test = "$eutils?db=$databank&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=$esearch";
    $result = file_get_contents($esearch_test);
    echo "Query: " . $esearch_test. "&lt;br \&gt;\n";
    echo "Result: " . $result . "&lt;br \&gt;\n";

    $xml = simplexml_load_string($result);
    echo "Count:&lt;br \&gt;\n";
    echo $xml-&gt;Count;
    $C =  $xml-&gt;Count;
    echo $C;
}
</code></pre>

CC BY-SA 4.0
2018-05-14T14:12:06.127Z
2018-05-14T14:12:06.127Z
Nemo
<p>I don't know anything about php, but when I copy your code into <a href="http://phptester.net" rel="nofollow noreferrer">here</a> it isn't happy about the variable name <code>$query.</code></p>

<p>This code appears to produce valid urls, but I can't make the <code>get</code> part work:</p>

<pre><code>$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils"; $db = "gene";$gene_symbols = array("A20","HBB","ABO");
$species = array("homo sapiens", "Escherichia coli","Saccharomyces cerevisiae"); $links = array();

foreach ($gene_symbols as $key =&gt; $GS){ 
   $SP = $species[$key];
   $SearchTerm = "$GS AND $SP [orgn] AND alive";

   $esearch = "$eutils/esearch.fcgi?db=$db&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=";

   echo $esearch.$SearchTerm;
}

http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=A20 AND homo sapiens [orgn] AND alivehttp://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=HBB AND Escherichia coli [orgn] AND alivehttp://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=ABO AND Saccharomyces cerevisiae [orgn] AND alive
</code></pre>

CC BY-SA 3.0
2018-04-27T11:35:25.430Z
2018-04-27T11:35:25.430Z
heathobrien
<p>I would like to create a URL that takes input from different arrays to generate a search. The data I would like to retrieve is from NCBI. Below you can find the code I wrote. But I can't get it to work; I am stuck. Thank you in advance.</p>

<p>I should have something like this as result but i got nothing. <a href="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=A20%20AND%20Homo%20sapiens%20%5borgn%5d%20AND%20alive%5bprop%5d" rel="nofollow noreferrer">https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=A20%20AND%20Homo%20sapiens%20%5borgn%5d%20AND%20alive%5bprop%5d</a></p>

<pre class="lang-php prettyprint-override"><code>$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils";
$db = "gene";
$gene_symbols = array("A20","HBB","ABO");
$species = array("homo sapiens", "Escherichia coli","Saccharomyces cerevisiae");
$links = array();
foreach ($gene_symbols as $key =&gt; $GS){
    $SP = $species[$key];
    $query = "$GS AND $SP [orgn] AND alive";
    $esearch = "$eutils/esearch.fcgi?db=$db&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=";
    #echo $esearch;
    $links[] = get($esearch.$query);
}
foreach($links as $lk){
    xml = simplexml_load_file($lk) or die("feed not loading");
}
var_dump(xml);
</code></pre>

<p>As output I got nothing.</p>

<p>But I would like to have this: <a href="https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=A20%20AND%20Homo%20sapiens%20%5borgn%5d%20AND%20alive%5bprop%5d" rel="nofollow noreferrer">https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&amp;retmode=xml&amp;retmax=10&amp;sort=relevance&amp;term=A20%20AND%20Homo%20sapiens%20%5borgn%5d%20AND%20alive%5bprop%5d</a>
And this for every gene. For some reason it put them all after each other and the foreach loop doesn't seem to work. I got no errors. </p>

<p>I run this code in php using visual studio code. The result should be visualised in the webbrowser (<a href="http://localhost/test.php" rel="nofollow noreferrer">http://localhost/test.php</a>).</p>

CC BY-SA 3.0
2018-04-27T09:59:45.463Z
Are you getting no output at all or simply the wrong output?
Devon Ryan
CC BY-SA 3.0
2018-04-27T10:04:00.063Z
Please [edit] your question and i) explain how you run this code and on what system; ii) tell us what language it is written in. Is that PHP? iii) Most importantly, explain exactly how it fails. Any errors? Any data at all?
terdon
CC BY-SA 3.0
2018-04-27T10:23:18.490Z
Can you print the links to see if they are valid? That would narrow down where the problem is
heathobrien
CC BY-SA 3.0
2018-04-27T11:05:25.383Z
As output I get this:http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=bccmtest&retmode=xml&retmax=10&sort=relevance&term=

But I would like to have this: https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=A20%20AND%20Homo%20sapiens%20%5borgn%5d%20AND%20alive%5bprop%5d
And this for every gene. For some reason it doesn't add the term and the foreach loop doesn't seem to work. I got no errors.

I run this code in php using visual studio code. The result should be visualised in the webbrowser (http://localhost/test.php).
Nemo
CC BY-SA 3.0
2018-04-27T11:14:25.837Z
The output you linked to is an error: `Invalid db name specified: bccmtest`. Your code shows you are setting the `db` to `gene`, so what do you get when you run the actual code from your question?
terdon
CC BY-SA 3.0
2018-04-27T11:24:29.930Z
I forgot to change the db name in the link I posted. I would like retrieve the search in the gene database from ncbi. The name of the database is gene.  If I do that i got "Empty term and query_key - nothing to do".
Nemo
CC BY-SA 3.0
2018-04-27T15:50:44.663Z
cross posted: https://www.biostars.org/p/311903/
Pierre
<phylogenetics>
Generating multiple NCBI URLs with eutils
# Generating multiple NCBI URLs with eutils

(Asked by: username_0 on 2018-04-27)

I would like to create a URL that takes input from different arrays to generate a search. The data I would like to retrieve is from NCBI. Below you can find the code I wrote. But I can't get it to work; I am stuck. Thank you in advance.
I should have something like this as result but i got nothing. https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=A20%20AND%20Homo%20sapiens%20%5borgn%5d%20AND%20alive%5bprop%5d
$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils";
$db = "gene";
$gene_symbols = array("A20","HBB","ABO");
$species = array("homo sapiens", "Escherichia coli","Saccharomyces cerevisiae");
$links = array();
foreach ($gene_symbols as $key => $GS){
    $SP = $species[$key];
    $query = "$GS AND $SP [orgn] AND alive";
    $esearch = "$eutils/esearch.fcgi?db=$db&retmode=xml&retmax=10&sort=relevance&term=";
    #echo $esearch;
    $links[] = get($esearch.$query);
}
foreach($links as $lk){
    xml = simplexml_load_file($lk) or die("feed not loading");
}
var_dump(xml);

As output I got nothing.
But I would like to have this: https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=A20%20AND%20Homo%20sapiens%20%5borgn%5d%20AND%20alive%5bprop%5d
And this for every gene. For some reason it put them all after each other and the foreach loop doesn't seem to work. I got no errors. 
I run this code in php using visual studio code. The result should be visualised in the webbrowser (http://localhost/test.php).

**Tags:** phylogenetics
> **Comments:**
> **username_1**: Are you getting no output at all or simply the wrong output?  
> **username_2**: Please [edit] your question and i) explain how you run this code and on what system; ii) tell us what language it is written in. Is that PHP? iii) Most importantly, explain exactly how it fails. Any errors? Any data at all?  
> **username_3**: Can you print the links to see if they are valid? That would narrow down where the problem is  
> **username_0**: As output I get this:http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=bccmtest&retmode=xml&retmax=10&sort=relevance&term=  But I would like to have this: https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=A20%20AND%20Homo%20sapiens%20%5borgn%5d%20AND%20alive%5bprop%5d And this for every gene. For some reason it doesn't add the term and the foreach loop doesn't seem to work. I got no errors.  I run this code in php using visual studio code. The result should be visualised in the webbrowser (http://localhost/test.php).  
> **username_2**: The output you linked to is an error: `Invalid db name specified: bccmtest`. Your code shows you are setting the `db` to `gene`, so what do you get when you run the actual code from your question?  
> **username_0**: I forgot to change the db name in the link I posted. I would like retrieve the search in the gene database from ncbi. The name of the database is gene.  If I do that i got "Empty term and query_key - nothing to do".  
> **username_4**: cross posted: https://www.biostars.org/p/311903/  

---

## 3 answers

### Answer by username_2 on 2018-04-27. Score: 0 | Accepted answer

I don't know PHP, so I'm sure there are better ways of doing this, but you had various issues in your code. This seems to be a working version of what you're after:
 $GS){ 
  $SP = $species[$key];
  $query = "$GS%20AND%20$SP%20[orgn]%20AND%20alive";

  $esearch = "$eutils/esearch.fcgi?db=$db&retmode=xml&retmax=10&sort=relevance&term=";

  echo "$esearch$query\n";
  $links[] = file_get_contents($esearch.$query);
  foreach($links as $lk){
    echo "|------ $lk ----|";
    $xml = simplexml_load_string($lk) or die("feed not loading");
    var_dump($xml);
  }
}
?>

There doesn't seem to be a get() function in PHP, you want file_get_contents. Also, your loops were nested incorrectly and you needed var_dump($xml) to dump the variable $xml not var_dump(xml); which tries to dump the string xml. You're not getting a file here but a string, so you need simplexml_load_string. Finally, you can't have unescaped whitespace in URLs. 
Next time, the way to debug this sort of thing is to have it print the URL it is generating and try to visit it manually. You would have seen the spaces and noticed that the working example you show uses %20 instead of space.


---

### Answer by username_0 on 2018-05-14. Score: 0

$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi";

   $databank = "gene";
   $gene_symbols = array("A20","HBB","ABO");

   $species = array("homo sapiens", "Escherichia coli","Saccharomyces cerevisiae");

foreach($gene_symbols as $value => $GS){
    $Species = $species[$value];


    $esearch = "$GS%AND%alive[prop]%AND+$Species[orgn]";
    $esearch_test = "$eutils?db=$databank&retmode=xml&retmax=10&sort=relevance&term=$esearch";
    $result = file_get_contents($esearch_test);
    echo "Query: " . $esearch_test. "\n";
    echo "Result: " . $result . "\n";

    $xml = simplexml_load_string($result);
    echo "Count:\n";
    echo $xml->Count;
    $C =  $xml->Count;
    echo $C;
}


---

### Answer by username_3 on 2018-04-27. Score: 0

I don't know anything about php, but when I copy your code into here it isn't happy about the variable name $query.
This code appears to produce valid urls, but I can't make the get part work:
$eutils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils"; $db = "gene";$gene_symbols = array("A20","HBB","ABO");
$species = array("homo sapiens", "Escherichia coli","Saccharomyces cerevisiae"); $links = array();

foreach ($gene_symbols as $key => $GS){ 
   $SP = $species[$key];
   $SearchTerm = "$GS AND $SP [orgn] AND alive";

   $esearch = "$eutils/esearch.fcgi?db=$db&retmode=xml&retmax=10&sort=relevance&term=";

   echo $esearch.$SearchTerm;
}

http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=A20 AND homo sapiens [orgn] AND alivehttp://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=HBB AND Escherichia coli [orgn] AND alivehttp://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=gene&retmode=xml&retmax=10&sort=relevance&term=ABO AND Saccharomyces cerevisiae [orgn] AND alive


---

