1
\$\begingroup\$

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:

<?php

$query = 'psoriasis';

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

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

$eFetchQueryParameters = array(
    'db' => 'pubmed',
    'retmax' => '500',
    'query_key' => (string) $eSearchQueryResults->QueryKey,
    'WebEnv' => (string) $eSearchQueryResults->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 => $value) {
    echo $value . "<br />";
}

?>
\$\endgroup\$
1

1 Answer 1

2
\$\begingroup\$

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.

<?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'         => 'pubmed',
        'term'       => '',
        'retmode'    => 'xml',
        'retstart'   => '0',
        'retmax'     => '500',
        'usehistory' => 'y'
    );

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

    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->query = $query;
    }

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

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

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

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

$query  = new MyQuery( 'psoriasis' );
$result = $query->get();
echo implode( '<br />', $result );

?>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.