HealthCheck in PHP

0

Build a PHP code that requests a web page, checks the returned HTTP code, and allows you to fetch a string from 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, would CURL be my best option for this purpose?

Note: For an understanding based on the context of my project I request that you access the link link.

    
asked by anonymous 26.09.2016 / 06:20

0 answers