gutocf/brasil-api-php-sdk

SDK for BrasilAPI

1.1.3 2024-03-08 18:19 UTC

README

CI Coverage Status PHPStan PHP Version Require Packagist Version License Total Downloads

PHP SDK for BrasilAPI.

Requirements

  • PHP ^8.0

Note: this package requires php:^8.0. For php:7.4 check out version 1.0.

Note: this package requires php:^8.0. For php:7.4 check out version 1.0.

Installation

You can install the package via composer:

composer require gutocf/brasil-api-php-sdk

Usage

First you need to get an instance of the BrasilAPI class:

use Gutocf\BrasilAPI\BrasilAPI;

$BrasilAPI = new BrasilAPI();

After that, you can use the service methods as follows. All services methods return an object or array of objects children of DataTransferObject from the spatie/data-transfer-object library.

CEP (V1)

//Gets a bank by its code.
$cep = $BrasilAPI->cepV1()->get('88045540');

CEP (V2)

//Gets a bank by its code.
$cep = $BrasilAPI->cepV2()->get('88045540');

Banks

//Gets all banks.
$banks = $BrasilAPI->banksV1()->getAll();

//Gets a bank by its code.
$bank = $BrasilAPI->banksV1()->get(1);

Holidays

//Gets holidays by year
$banks = $BrasilAPI->holidaysV1()->getByYear(2022);

CNPJ

//Gets information about a CNPJ
$banks = $BrasilAPI->cnpjV1()->get('39729684000100');

DDD

//Gets information about a DDD
$ddd = $BrasilAPI->dddV1()->get(48);

FIPE

Reference Tables

use Gutocf\BrasilAPI\Entity\V1\Fipe\Enum\VehicleType;

//Gets all reference tables
$referenceTables = $BrasilAPI->fipeV1()->getAllReferenceTables();

Vehicles

//Gets vehicle price information by FIPE code
$vehicles = $BrasilAPI->fipeV1()->getAllVehicleByCode('003281-6');
//Gets vehicle price information by FIPE code for a specific table
$vehicles = $BrasilAPI->fipeV1()->getAllVehicleByCode('003281-6', 123);

Brands

use Gutocf\BrasilAPI\Entity\V1\Fipe\Enum\VehicleType;

//Gets all brands
$brands = $BrasilAPI->fipeV1()->getAllBrandsByType();
//Gets all brands by vehicle type
$brands = $BrasilAPI->fipeV1()->getAllBrandsByType(VehicleType::CARS());
//Gets all brands by vehicle type for a specific table
$brands = $BrasilAPI->fipeV1()->getAllBrandsByType(VehicleType::CARS(), 123);

IBGE

Cities by State

$cities = $BrasilAPI->ibgeV1()->getCitiesByState('SC');

States

$states = $BrasilAPI->ibgeV1()->getAllStates();

State by initials or code

$state = $BrasilAPI->ibgeV1()->getState('sc');
//OR
$state = $BrasilAPI->ibgeV1()->getState(42);

Error handling

HTTP errors communicating with the Brasil API will throw exceptions as follows:

Status code Exception
400 Gutocf\BrasilAPI\Exception\NotFoundException
404 Gutocf\BrasilAPI\Exception\BadRequestException
500 Gutocf\BrasilAPI\Exception\InternalServerErrorException