PHP SDK for BrasilAPI.
- 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.
You can install the package via composer:
composer require gutocf/brasil-api-php-sdk
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.
//Gets a bank by its code.
$cep = $BrasilAPI->cepV1()->get('88045540');
//Gets a bank by its code.
$cep = $BrasilAPI->cepV2()->get('88045540');
//Gets all banks.
$banks = $BrasilAPI->banksV1()->getAll();
//Gets a bank by its code.
$bank = $BrasilAPI->banksV1()->get(1);
//Gets holidays by year
$banks = $BrasilAPI->holidaysV1()->getByYear(2022);
//Gets information about a CNPJ
$banks = $BrasilAPI->cnpjV1()->get('39729684000100');
//Gets information about a DDD
$ddd = $BrasilAPI->dddV1()->get(48);
use Gutocf\BrasilAPI\Entity\V1\Fipe\Enum\VehicleType;
//Gets all reference tables
$referenceTables = $BrasilAPI->fipeV1()->getAllReferenceTables();
//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);
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);
$cities = $BrasilAPI->ibgeV1()->getCitiesByState('SC');
$states = $BrasilAPI->ibgeV1()->getAllStates();
$state = $BrasilAPI->ibgeV1()->getState('sc');
//OR
$state = $BrasilAPI->ibgeV1()->getState(42);
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 |