PHP code example of azumamagus / get-cnpj

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

    

azumamagus / get-cnpj example snippets




etCNPJ\CnpjClient;

$result = (new CnpjClient())->get('03.312.791/0001-83');

if ($result->success) {
    echo $result->data->razaoSocial . PHP_EOL;
    echo $result->data->endereco?->getEnderecoCompleto() . PHP_EOL;
    echo 'Provedor: ' . $result->data->provedor . PHP_EOL;
} else {
    echo $result->errorMessage . PHP_EOL;
}

use GetCNPJ\CnpjClient;

$client = new CnpjClient();
$result = $client->get('03312791000183');

if (!$result->success) {
    foreach ($result->errors as $error) {
        echo "{$error->providerName}: {$error->errorMessage}" . PHP_EOL;
    }

    return;
}

$empresa = $result->data;

echo $empresa->cnpj . PHP_EOL;
echo $empresa->razaoSocial . PHP_EOL;
echo $empresa->nomeFantasia . PHP_EOL;
echo $empresa->situacao . PHP_EOL;

foreach ($empresa->telefones as $telefone) {
    echo $telefone . PHP_EOL;
}

foreach ($empresa->inscricoesEstaduais as $inscricao) {
    echo $inscricao . PHP_EOL;
}



namespace App\Http\Controllers;

use GetCNPJ\CnpjClient;
use Illuminate\Http\JsonResponse;

final class EmpresaController extends Controller
{
    public function show(string $cnpj, CnpjClient $client): JsonResponse
    {
        return response()->json($client->get($cnpj));
    }
}

$client = app(\GetCNPJ\CnpjClient::class);

use GetCNPJ\Integrations\Laravel\Facades\GetCNPJ;
use GetCNPJ\ProviderType;

$result = GetCNPJ::get('03.312.791/0001-83');

$brasilApi = GetCNPJ::getFromProvider(
    '03312791000183',
    ProviderType::BrasilAPI,
);



namespace App\Controllers;

use CodeIgniter\HTTP\ResponseInterface;

final class Empresa extends BaseController
{
    public function show(string $cnpj): ResponseInterface
    {
        $result = service('getCnpj')->get($cnpj);

        return $this->response->setJSON($result);
    }
}

$client = single_service('getCnpj');

$client = \GetCNPJ\Config\Services::getCnpj();

use GetCNPJ\CnpjClient;
use GetCNPJ\ProviderType;

$client = new CnpjClient();

$result = $client->getFromProvider(
    '03312791000183',
    ProviderType::BrasilAPI,
);

use GetCNPJ\CnpjClient;
use GetCNPJ\CnpjClientOptions;

$options = new CnpjClientOptions(
    timeout: 60,
    maxRequestsPerMinute: 5,
    enableCnpjWs: true,
    enableReceitaWs: true,
    enableBrasilApi: true,
    enableCnpja: false,
);

$client = new CnpjClient(options: $options);

use GetCNPJ\CnpjClient;
use GuzzleHttp\Client;

$http = new Client([
    'timeout' => 15,
    'proxy' => 'http://proxy.exemplo:8080',
    'http_errors' => false,
]);

$client = new CnpjClient(httpClient: $http);

$result->success;         // bool
$result->data;            // ?CnpjData
$result->errorMessage;    // ?string
$result->errors;          // ProviderError[]
$result->getFailedProviders();

echo json_encode(
    $result,
    JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES,
);

use GetCNPJ\Exceptions\InvalidCnpjException;

try {
    $result = (new CnpjClient())->get('00.000.000/0000-00');
} catch (InvalidCnpjException $exception) {
    echo $exception->getMessage();
}

if (!$result->success) {
    echo $result->errorMessage . PHP_EOL;

    foreach ($result->errors as $error) {
        echo "{$error->providerName}: {$error->errorMessage}" . PHP_EOL;
    }
}
bash
php artisan vendor:publish --tag=getcnpj-config
json
{
  "success": true,
  "data": {
    "cnpj": "03.312.791/0001-83",
    "razaoSocial": "SOS SYSTEM TECNOLOGIA EM INFORMATICA LTDA",
    "situacao": "Ativa",
    "telefones": ["(14) 3206-2096"],
    "provedor": "CNPJWS"
  },
  "errorMessage": null,
  "errors": []
}
bash
git clone https://github.com/azumamagus/GetCNPJ-php.git
cd GetCNPJ-php
composer install
composer test