Scraping PubMed query results
In my humble opinion, the version below is more elegant, easier to read and to maintain.

It uses a class to keep things more organized.

I would consider the code below as a starting point to start being improved, with things like error checking, etc.

I hope you like, and maybe learn something from it.

&lt;?php

class MyQuery
{
    public $query = '';

    public $search_url = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?';
    public $fetch_url  = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?';

    public $search_parameters = array(
        'db'         =&gt; 'pubmed',
        'term'       =&gt; '',
        'retmode'    =&gt; 'xml',
        'retstart'   =&gt; '0',
        'retmax'     =&gt; '500',
        'usehistory' =&gt; 'y'
    );

    public $fetch_parameters = array(
        'db'        =&gt; 'pubmed',
        'retmax'    =&gt; '500',
        'query_key' =&gt; '',
        'WebEnv'    =&gt; ''
    );

    public $search_results;
    public $fetch_results;
    public $matches = array();

    public $match_regex = '/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/';

    public __constructor( $query )
    {
        $this-&gt;query = $query;
    }

    public search()
    {
        $this-&gt;search_parameters['term'] = $this-&gt;query;
        $url = $this-&gt;search_url . http_build_query( $this-&gt;search_parameters );
        $this-&gt;search_results = simplexml_load_file( $url );
    }

    public fetch()
    {
        $this-&gt;fetch_parameters['query_key'] = (string) $this-&gt;search_results-&gt;QueryKey;
        $this-&gt;fetch_parameters['WebEnv']    = (string) $this-&gt;search_results-&gt;WebEnv
        $url = $this-&gt;fetch_url . http_build_query( $this-&gt;fetch_parameters );
        $this-&gt;fetch_results = file_get_contents( $url );
    }

    public match()
    {
        $matches = array();
        preg_match_all( $this-&gt;match_regex, $this-&gt;fetch_results, $matches );
        $this-&gt;matches = array_values( $matches[0] );
    }

    public get()
    {
        $this-&gt;search();
        $this-&gt;fetch();
        $this-&gt;match();
        return $this-&gt;matches;
    }
}

$query  = new MyQuery( 'psoriasis' );
$result = $query-&gt;get();
echo implode( '&lt;br /&gt;', $result );

?&gt;