PHP code example of devaction / cnpja-php

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

    

devaction / cnpja-php example snippets


use Cnpja\CnpjaClient;

$client = new CnpjaClient('sua-chave-api');

$office = $client->getOffice('37335118000180');

echo $office->taxId;            // "37335118000180"
echo $office->alias;            // "CNPJA"
echo $office->status->text;     // "Ativa"
echo $office->address->city;    // "São Paulo"
echo $office->address->state;   // "SP"
echo $office->phones[0]->area;  // "11"

use Cnpja\Laravel\Facades\Cnpja;

$office = Cnpja::getOffice('37335118000180');

use Cnpja\CnpjaClient;

class EmpresaController extends Controller
{
    public function show(CnpjaClient $cnpja, string $cnpj)
    {
        $office = $cnpja->getOffice($cnpj);

        return response()->json([
            'nome'     => $office->company?->name,
            'situacao' => $office->status->text,
            'cidade'   => $office->address->city,
        ]);
    }
}

$saldo = $client->getCredit();

$saldo->perpetual;   // int — créditos permanentes
$saldo->transient;   // int — créditos temporários

$zip = $client->getZip('01452922');

$zip->code;          // "01452922"
$zip->city;          // "São Paulo"
$zip->state;         // "SP"
$zip->street;        // "Avenida Brigadeiro Faria Lima"
$zip->district;      // "Jardim Paulistano"
$zip->municipality;  // 3550308 (código IBGE)
$zip->updated;       // "2024-06-05T17:52:39.136Z"

$company = $client->getCompany('37335118');

$company->id;                    // 37335118
$company->name;                  // "CNPJA TECNOLOGIA LTDA"
$company->equity;                // 1000.0 (capital social)
$company->nature->text;          // "Sociedade Empresária Limitada"
$company->size->acronym;         // "ME"
$company->jurisdiction;          // "Uniao"
$company->simples?->optant;      // true
$company->simei?->since;         // "2020-06-05"

// Sócios
foreach ($company->members as $member) {
    $member->person->name;       // "João Silva"
    $member->role->text;         // "Sócio-Administrador"
    $member->since;              // "2020-06-05"
}

$office = $client->getOffice('37335118000180');

use Cnpja\Params\GetOfficeParams;
use Cnpja\Params\CacheParams;

$office = $client->getOffice('37335118000180', new GetOfficeParams(
    simples: true,           // +1 crédito — inclui Simples/MEI
    simplesHistory: true,    // +1 crédito — inclui histórico Simples/MEI
    registrations: 'ALL',        // +1 crédito — inscrições estaduais (ORIGIN, ALL ou UFs)
    registrationsSource: 'CCC', // fonte: AUTO, CCC ou SINTEGRA
    suframa: true,               // +1 crédito — inclui dados da SUFRAMA
    geocoding: true,             // +1 crédito — inclui lat/long do endereço
    links: 'RFB_CERTIFICATE,SIMPLES_CERTIFICATE,CCC_CERTIFICATE,SUFRAMA_CERTIFICATE,OFFICE_MAP,OFFICE_STREET',
    cache: new CacheParams(
        strategy: 'CACHE_IF_ERROR',
        maxAge: 45,
        maxStale: 365,
    ),
));

$office->taxId;                         // "37335118000180"
$office->updated;                       // "2024-06-05T..."
$office->alias;                         // "CNPJA"
$office->founded;                       // "2020-06-05"
$office->head;                          // true (matriz)
$office->status->id;                    // 2
$office->status->text;                  // "Ativa"
$office->statusDate;                    // "2020-06-05"

// Empresa mãe
$office->company?->name;                // "CNPJA TECNOLOGIA LTDA"
$office->company?->equity;              // 1000.0
$office->company?->nature->text;        // "Sociedade Empresária Limitada"
$office->company?->size->acronym;       // "ME"
$office->company?->simples?->optant;    // true
$office->company?->members[0]->person->name;  // "João Silva"

// Endereço
$office->address->street;               // "Avenida Brigadeiro Faria Lima"
$office->address->number;               // "2369"
$office->address->district;             // "Jardim Paulistano"
$office->address->city;                 // "São Paulo"
$office->address->state;                // "SP"
$office->address->zip;                  // "01452922"
$office->address->latitude;             // -23.5774994 (requer geocoding: true)
$office->address->longitude;            // -46.6864608 (requer geocoding: true)

// Contatos
$office->phones[0]->area;               // "11"
$office->phones[0]->number;             // "971564144"
$office->emails[0]->address;            // "[email protected]"
$office->emails[0]->domain;             // "cnpja.com"

// Atividades
$office->mainActivity->id;              // 6311900
$office->mainActivity->text;            // "Tratamento de dados..."
$office->sideActivities[0]->text;       // ...

// Situação especial
$office->reason?->text;                 // nullable
$office->special?->text;                // nullable
$office->specialDate;                   // nullable

// Inscrições estaduais (requer registrations)
foreach ($office->registrations as $reg) {
    $reg->number;      // "0962101427"
    $reg->state;       // "RS"
    $reg->enabled;     // true
    $reg->status->text; // "Ativa"
    $reg->type->text;   // "IE Normal"
}

// Links de comprovantes (requer links param)
$office->links?->rfbCertificate;        // URL do PDF
$office->links?->simplesCertificate;    // URL do PDF
$office->links?->cccCertificate;        // URL do PDF
$office->links?->suframaCertificate;    // URL do PDF
$office->links?->officeMap;             // URL da imagem PNG
$office->links?->officeStreet;          // URL da imagem PNG

use Cnpja\Params\SearchOfficesParams;

$resultado = $client->searchOffices(new SearchOfficesParams(
    aliasIn: 'teuto',
    stateIn: 'SP,RJ',
    statusIn: '2',          // 2 = Ativa
    simplesOptant: true,
    limit: 10,
));

$resultado->count;          // total de registros
$resultado->next;           // token para próxima página
$resultado->records;        // OfficeDto[]

// Paginação
$pagina2 = $client->searchOffices(new SearchOfficesParams(
    token: $resultado->next,
));

// Retornam string com bytes PNG
$png = $client->getOfficeMap('37335118000180', ['zoom' => 17, 'width' => 640]);
$png = $client->getOfficeStreetView('37335118000180', ['fov' => 90, 'width' => 640]);

// Salvar em disco
file_put_contents('mapa.png', $png);

use Cnpja\Params\GetRfbParams;
use Cnpja\Params\CacheParams;

$rfb = $client->getRfb('37335118000180', new GetRfbParams(
    cache: new CacheParams(strategy: 'CACHE_IF_ERROR'),
));

$rfb->taxId;                // "37335118000180"
$rfb->name;                 // "CNPJA TECNOLOGIA LTDA"
$rfb->equity;               // 1000.0
$rfb->nature->text;         // "Sociedade Empresária Limitada"
$rfb->size->acronym;        // "ME"
$rfb->jurisdiction;         // "Uniao"
$rfb->status->text;         // "Ativa"
$rfb->address->city;        // "São Paulo"
$rfb->members[0]->person->name;  // "João Silva"

// PDF do comprovante (bytes brutos)
$pdf = $client->getRfbCertificate('37335118000180', pages: 'REGISTRATION,MEMBERS');
file_put_contents('rfb.pdf', $pdf);

use Cnpja\Params\GetSimplesParams;
use Cnpja\Params\CacheParams;

$simples = $client->getSimples('37335118000180', new GetSimplesParams(
    history: true,           // +1 crédito — inclui histórico de períodos
    cache: new CacheParams(strategy: 'CACHE_IF_ERROR'),
));

$simples->taxId;              // "37335118000180"
$simples->simples->optant;    // true
$simples->simples->since;     // "2020-06-05"
$simples->simei->optant;      // false

// Histórico de exclusões
foreach ($simples->simples->history as $entry) {
    $entry->from;   // "2012-12-26"
    $entry->until;  // "2013-12-31"
    $entry->text;   // "Excluída por Ato Administrativo..."
}

// PDF do comprovante
$pdf = $client->getSimplesCertificate('37335118000180');

use Cnpja\Params\GetCccParams;
use Cnpja\Params\CacheParams;

$ccc = $client->getCcc('37335118000180', 'ALL', new GetCccParams(
    source: 'CCC',
    cache: new CacheParams(strategy: 'CACHE_IF_ERROR'),
));

$ccc->taxId;          // "37335118000180"
$ccc->originState;    // "PR"

foreach ($ccc->registrations as $reg) {
    $reg->number;        // "0962101427"
    $reg->state;         // "RS"
    $reg->enabled;       // true
    $reg->statusDate;    // "2021-01-21"
    $reg->status->text;  // "Bloqueado como destinatário na UF"
    $reg->type->text;    // "IE Substituto Tributário"
}

// PDF do comprovante
$pdf = $client->getCccCertificate('37335118000180', state: 'SP');

use Cnpja\Params\GetSuframaParams;
use Cnpja\Params\CacheParams;

$suframa = $client->getSuframa('37335118000180', new GetSuframaParams(
    cache: new CacheParams(strategy: 'CACHE_IF_ERROR'),
));

$suframa->taxId;           // "37335118000180"
$suframa->number;          // "200400029"
$suframa->name;            // "CNPJA TECNOLOGIA LTDA"
$suframa->approved;        // true
$suframa->approvalDate;    // "2021-01-01"
$suframa->status->text;    // "Ativa"

foreach ($suframa->incentives as $incentive) {
    $incentive->tribute; // "IPI"
    $incentive->benefit; // "Isenção"
    $incentive->purpose; // "Consumo Interno, Industrialização..."
    $incentive->basis;   // "Decreto 7.212 de 2010 (Art. 81)"
}

// PDF do comprovante
$pdf = $client->getSuframaCertificate('37335118000180');

use Cnpja\Exceptions\UnauthorizedException;
use Cnpja\Exceptions\NotFoundException;
use Cnpja\Exceptions\RateLimitException;
use Cnpja\Exceptions\ValidationException;
use Cnpja\Exceptions\ServiceUnavailableException;

try {
    $office = $client->getOffice('37335118000180');
} catch (UnauthorizedException $e) {
    // 401 — chave de API inválida ou ausente
    echo $e->getMessage();
} catch (NotFoundException $e) {
    // 404 — CNPJ não encontrado
    echo $e->getMessage();
} catch (RateLimitException $e) {
    // 429 — créditos insuficientes ou limite por minuto excedido
    echo "Necessário: {$e->

use Cnpja\Params\CacheParams;

$cache = new CacheParams(
    strategy: 'CACHE_IF_ERROR',
    maxAge: 45,      // dias — idade máxima do cache aceita
    maxStale: 365,   // dias — idade máxima aceita em caso de falha online
    sync: false,     // aguardar compensação síncrona de créditos
);
bash
composer 
bash
php artisan vendor:publish --tag=cnpja-config