Receive error in cUrl request

1

I'm trying to get the error in a return from cUrl

 ini_set("display_errors",true);
 ini_set("display_startup_erros",1);
 error_reporting(E_ALL && E_NOTICE);
 error_reporting( E_ALL | E_STRICT ); // PHP 5.3
 error_reporting( E_ALL ); // Todas as outras versões 

  class consultaCep {

    private $phpUtil;
    private $erro = "";

    public function __construct($_phpUtil) {
         $this->phpUtil = $_phpUtil;
    }

    function consultarCepViaCep ($_cep)    {

        $_cep = $this->phpUtil->limpaCaracters($_cep);

        $urliaCep = sprintf('http://viacep.com.br/ws/%s/json/ ', $_cep);

          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $urliaCep);
          curl_setopt($ch, CURLOPT_FAILONERROR, true);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

          $data = curl_exec($ch);

          $data = json_decode($data);

          if(isset($data->erro)) {

              $this->erro = $data->erro;

          } else {

              return $data;

          }

    }

    public function getErro () {
        return $this->erro;
    }

}

When I do:

require_once "_controlls/_util/PhpUtil.php";
$phpUtil = new PhpUtil();

$_POST["cep"] = "11111111";
$consultaCep = new consultaCep($phpUtil);
$consultarCep = $consultaCep->consultarCepViaCep($_POST["cep"]);      

if($consultaCep->getErro() != "") {

  print "Erro: ".$consultaCep->getErro();

} else {

  print "<pre>";
  print_r($consultarCep);
  print "</pre>";  
}

When it is correct, the return is normal.

But when there is an invalid zip error it gives type 1 error

Ma when the error is of incomplete cep does not give error or complete the data

    
asked by anonymous 07.06.2016 / 15:24

2 answers

0

The problem is that curl_init is not an exception type. CURL has no exception handling. Your approach must be different. You'll have to extend the class Exceptions to do something similar to this .

    
07.06.2016 / 16:03
0

Well, I decided that way. But I think there must be a more correct way to verify this. If it exists, please help me. Because using the post office webservice is taking up to 30 seconds to drop the return.

Errors

 ini_set("display_errors",true);
 ini_set("display_startup_erros",1);
 error_reporting(E_ALL && E_NOTICE);
 error_reporting( E_ALL | E_STRICT ); // PHP 5.3
 error_reporting( E_ALL ); // Todas as outras versões 

Class

  class consultaCep {

    private $phpUtil;
    private $erro = "";

    public function __construct($_phpUtil) {
         $this->phpUtil = $_phpUtil;
    }

    function consultarCepViaCep ($_cep)    {

        $_cep = $this->phpUtil->limpaCaracters($_cep);

        $urliaCep = sprintf('http://viacep.com.br/ws/%s/json/', $_cep);

          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $urliaCep);
          curl_setopt($ch, CURLOPT_FAILONERROR, true);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
          $data = curl_exec($ch);         

          if($data==false) {

              $this->erro = "Erro de interoperação! Verifique o CEP digitado";

          } else {

              $data = json_decode($data);

              if (isset($data->erro)) {

                   switch ($data->erro==1)  {
                       case  1 : $erro = "CEP não encontrado";
                       break;
                       case  1 : $erro = "Sistema Falhou";
                       break;
                  }

                  $this->erro = $erro;

              } else {

              return $data;

              }           

          }

    }

    public function getErro () {
        return $this->erro;
    }

}

I do:     require_once "_controlls / _util / PhpUtil.php"; // just to remove the dots and dashes     $ phpUtil = new PhpUtil ();

$_POST["cep"] = "11111111";
$consultaCep = new consultaCep($phpUtil);
$consultarCep = $consultaCep->consultarCepViaCep($_POST["cep"]);      

if($consultaCep->getErro() != "") {

  print $consultaCep->getErro();

} else {

  print "<pre>";
  print_r($consultarCep);
  print "</pre>";  
}
    
07.06.2016 / 18:42