PHP code example of zipcodestack / zipcodestack-php

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

    

zipcodestack / zipcodestack-php example snippets



use ZipCodeStack\Client;

// Option 1: via env var
// put in your shell or server env: export ZIPCODESTACK_API_KEY="your_key"
$client = new Client();

// Option 2: pass API key directly
$client = new Client('your_key');

// Option 3: query param auth
$client = new Client('your_key', ['auth_method' => 'query']);


declare(strict_types=1);

use ZipCodeStack\Client;
use ZipCodeStack\Exception\AuthenticationException;
use ZipCodeStack\Exception\RateLimitException;
use ZipCodeStack\Exception\ApiException;

$client = new Client(getenv('ZIPCODESTACK_API_KEY') ?: 'your_key');

// Search endpoint
$result = $client->search([
    'query' => '10001', // or any supported parameter; see docs
]);

// Distance endpoint
$distance = $client->distance('10001', '94105', [
    'unit' => 'mi', // or 'km'
]);

// Status endpoint
$status = $client->status();


try {
    $result = $client->search(['query' => '10001']);
} catch (AuthenticationException $e) {
    // invalid or missing API key
} catch (RateLimitException $e) {
    // too many requests; consider $e->getRetryAfter()
} catch (ApiException $e) {
    // other API or network errors
}

$client = new Client('your_key', [
    'base_uri' => 'https://api.zipcodestack.com',
    'api_base_path' => '/v1',
    'timeout' => 10.0,
    'connect_timeout' => 5.0,
    'headers' => [ 'X-Custom' => 'value' ],
    'auth_method' => 'header', // or 'query'
]);
bash
composer