PHP code example of canducci / zipcode

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

    

canducci / zipcode example snippets


'providers' => [
    // ...,
    Canducci\ZipCode\Providers\ZipCodeServiceProvider::class,
    Canducci\ZipCode\Providers\ZipCodeAddressServiceProvider::class,
],

'aliases' => [
    // ...,
    'ZipCode'   => Canducci\ZipCode\Facades\ZipCode::class,
    'Address'   => Canducci\ZipCode\Facades\ZipCodeAddress::class,
],

php artisan vendor:publish

use Canducci\ZipCode\Facades\ZipCode;

$zipCodeInfo = ZipCode::find('01414-001');

$zipCodeInfo = zipcode('01414000');

use Canducci\ZipCode\Contracts\ZipCodeContract;

public function index(ZipCodeContract $zipcode)
{
      $zipCodeInfo = $zipcode->find('01414-000');
}

use Canducci\ZipCode\ZipCodeTrait;

class WelcomeController extends Controller
{
    use ZipCodeTrait;

    public function index()
    {
        $zipCodeInfo =	$this->zipcode('01414000');
    }
}

$zipCodeInfo = ZipCode::find('01414000'); // Facade

$zipCodeInfo = $zipcode->find('01414-000'); // Contract

$zipCodeInfo = zipcode('01414000'); // Helper

$zipCodeInfo = $this->zipcode('01414-000'); // Trait

$zipCodeInfo = ZipCode::find('01414000', true); // Facade

$zipCodeInfo = $zipcode->find('01414-000', true); // Contract

$zipCodeInfo = zipcode('01414000', true); // Helper

$zipCodeInfo = $this->zipcode('01414-000', true); // Trait

if ($zipCodeInfo) // null or ZipCodeInfo
{
    $arr = $zipCodeInfo->getArray(); // Array

    $json = $zipCodeInfo->getJson(); // Json

    $obj = $zipCodeInfo->getObject(); // Object
}

if ($zipCodeInfo)
{
    $arr = $zipCodeInfo->getArray();
    /*
    Array
    (
        [cep] => 01414-001
        [logradouro] => Rua Haddock Lobo
        [bairro] => Cerqueira César
        [localidade] => São Paulo
        [uf] => SP
        [ibge] => 3550308,
        [complemento] =>
        [gia] => 1004,
        [siafi] => 0
        [ddd] = 11
    )
    */
}

if ($zipCodeInfo)
{
    $json = $zipCodeInfo->getJson();
    /*
    {
        "cep": "01414-001",
        "logradouro": "Rua Haddock Lobo",
        "bairro": "Cerqueira César",
        "localidade": "São Paulo",
        "uf": "SP",
        "ibge": "3550308",
        "complemento": ""
        "gia": 1004,
        "ddd": "11",
        "siafi": 0
    }
    */
}

if ($zipCodeInfo)
{
    $obj = $zipCodeInfo->getObject();
    /*
    stdClass Object
    (
        [cep] => 01414-001
        [logradouro] => Rua Haddock Lobo
        [bairro] => Cerqueira César
        [localidade] => São Paulo
        [uf] => SP
        [ibge] => 3550308
        [complemento] =>
        [gia] => 1004
        [siafi] => 0
        [ddd] = 011
    )
    */
}

public function get(Request $request)
{
    $uf = $request->get('uf');
    $city = $request->get('cidade');
    $address = $request->get('endereco')
    $zipcodeaddressinfo = zipcodeaddress($uf, $city, $address);
    if ($zipcodeaddressinfo)
        return $zipcodeaddressinfo->getJson();
    return Response::json(['error' => 1]);
}

use Canducci\ZipCode\ZipCodeUf;

$lists = ZipCodeUf::lists();