PHP code example of andersonef / cep-promise-php

1. Go to this page and download the library: Download andersonef/cep-promise-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andersonef / cep-promise-php example snippets


$promise = (new CepPromise('83322170'))->fetch(); 

// ASSÍNCRONO: Com a promise em mãos, eu posso tanto:
$promise->then(function($endereco) {
    // sua lógica aqui
});

// SÍNCRONO: 
$endereco = $promise->wait();

/** Endereço: 
 * {
  "cep": "83322170",
  "street": "Rua Rio Tocantins",
  "neighborhood": "Weissópolis",
  "city": "Pinhais",
  "state": "PR"
}
*/

class CepFromDatabase implements ServiceInterface
{
    public function fetch($cep): ResponseAddress
    {
        // .. your custom logic here
        $response = new ResponseAddress(
            $cep,
            $rua,
            $bairro,
            $cidade,
            $estado
        );
        return $response;
    }
}


$cepFromDatabaseService = new CepFromDatabase();
$cepPromise = new CepPromise();

$endereco = $cepPromise
    ->clearServices() // OPCIONAL: elimina os services padrão (correios, viacep e widenet)
    ->appendService($cepFromDatabaseService)
    ->fetch('83322170'); // Agora a classe irá usar seu service customizado!
bash
composer