Healthcheck in PHP

Asked

Viewed 195 times

0

I built a PHP code that requires a web page, checks the returned HTTP code and lets you search for a string present in the html source.

I used CURL for this. The code is basically this:

class HealthCheck
{
    /**
     * Apontamento para uma instância de Curl
     * 
     * @access private
     * @var object
     */
    private $curl;

    /**
     * Conteúdo da URL consultada
     *
     * @var string 
     */
    private $html;

    /**
     * Atribui alguns valores considerados padrão
     * 
     * @access public
     */
    public function __construct()
    {
        $this->curl = curl_init();
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false);
    }

    /**
     * Atribui a URL alvo
     * 
     * @param string url
     */    
    public function setURL($url)
    {
        curl_setopt($this->curl, CURLOPT_URL, $url);
    }

    /**
     * Define o agente a ser utilizado
     * 
     * @param string agent
     */  
    public function setAgent($agent = "PHP Health Check")
    {
        curl_setopt($this->curl, CURLOPT_USERAGENT, $agent);
    }

    /**
     * Define regras de redirecionamento de URL
     * 
     * @param bool redirecionar
     * @param int numRedirect
     * @param bool refresh
     */  
    public function setRedirect($redirect = true, $numRedirect = 5, $refresh = true)
    {
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, $redirect);
        curl_setopt($this->curl, CURLOPT_MAXREDIRS, $numRedirect);
        curl_setopt($this->curl, CURLOPT_AUTOREFERER, $refresh);
    }

    /**
     * Define o tempo máximo de execução do processo
     * 
     * @param int timeout
     */  
    public function setTimeOut($timeout = 10)
    {
        curl_setopt($this->curl, CURLOPT_TIMEOUT, $timeout);
    }
    /**
     * Executa a consulta a URL alvo
     * 
     * @return int
     */  
    public function run()
    {
        $this->html = curl_exec($this->curl);
        $code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
        curl_close($this->curl);
        return $code;
    }

    /**
     * Verifica se determinada string existe no contexto da página consultada
     * 
     * @param string $buscar
     * @return bool
     */
    public function buscarString($buscar)
    {
        // se a string de buscar for encontrada retorna true
        return (strpos(strtolower($this->html), strtolower($buscar)) === false) ? false : true;
    }
}

My question is, for that purpose the CURL would be my best option?

Note: For an understanding based on the context of my project I ask you to access the link https://github.com/fabiojaniolima/PGR

  • Hello Fabio, can you bring here what is important about this link on Github? Otherwise in a few months that link may no longer exist and the question is incomplete...

  • It would be the Healthcheck code I posted above. The Github link is if someone wants to better understand the context of the idea

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.