PHP code example of eberfreitas / correios

1. Go to this page and download the library: Download eberfreitas/correios 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/ */

    

eberfreitas / correios example snippets

 php
$correios = new Correios\Correios();
 php
$adaptador = new MinhaClasseHttp;

$correios = new Correios\Correios([
    'usuario' => 'joao',
    'senha' => 'segredo',
    'http_adapter' => $adaptador
]);
 php
$endereco = $correios->endereco('70150900');

print_r($endereco);
 php
$eventos = $correios->rastreamento('PE024442250BR');

print_r($eventos);
 php
$calculado = $correios->calculaFrete([
    'usuario' => null,
    'senha' => null,
    'servicos' => [Correios\Correios::PAC],
    'cep_origem' => '08820400',
    'cep_destino' => '21832150',
    'peso' => 0.3,
    'formato' => Correios\Correios::CAIXA,
    'comprimento' => 16,
    'altura' => 2,
    'largura' => 11,
    'diametro' => 5,
    'mao_propria' => false,
    'valor_declarado' => 0,
    'aviso_recebimento' => false
]);

print_r($calculado);
 php
echo $calculado[0]['codigo']; //41106
echo $calculado[0]->valor; //15.5
 php
$entrega = $calculado[0]->calculaEntrega(new DateTime('now'));

print_r($entrega);

DateTime Object
(
    [date] => 2015-05-18 00:00:00
    [timezone_type] => 3
    [timezone] => UTC
)
 php
echo $calculado[0]->formataValor('valor');
 php
echo $calculado[0]->formataValor('valor', talse); //15,50
 php
$config = []; //a configuração de sua chamada aqui...
$calculado = $correios->calculaFrete($config, false);