Scraping PubMed query results How can I make the following PHP code better, more efficient, shorter, elegant, etc?  While it already works, I am still learning PHP and want to improve upon my current code:

&lt;?php

$query = 'psoriasis';

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

$eSearchQueryResults = simplexml_load_file('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?' . http_build_query($eSearchQueryParameters));

$eFetchQueryParameters = array(
    'db' =&gt; 'pubmed',
    'retmax' =&gt; '500',
    'query_key' =&gt; (string) $eSearchQueryResults-&gt;QueryKey,
    'WebEnv' =&gt; (string) $eSearchQueryResults-&gt;WebEnv,
);

$eFetchURL = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?' . http_build_query($eFetchQueryParameters);
$matches = array();
preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/', file_get_contents($eFetchURL), $matches);

foreach ($matches[0] as $key =&gt; $value) {
    echo $value . "&lt;br /&gt;";
}

?&gt;
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;