Receive error in Curl request

Asked

Viewed 793 times

-2

I am 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 comes normal.

But when there is invalid zip error gives type 1 error

Ma when the error is of incomplete zip code does not give error and does not complete the data

  • curl_init is a kind of exception? the catch should capture a specific count passes straight into the block.

  • in the case of which the output? curl_exec? I have tendered, did not give!

2 answers

0

The problem is that curl_init is not a type of exception. CURL no exception manipulation. Your approach will have to be different. You will have to extend the Exceptions class by doing something similar to this.

-1

Well, I solved it like this. But I think there must be a more correct way to verify it. If there is, please help me. Because using the Post Office webservice is taking up to 30 seconds to drop the return.

Mistakes

 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;
    }

}

Faço: require_once "_controlls/_util/Phputil.php"; // only to remove points and strokes $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>";  
}

Browser other questions tagged

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