PHP code example of pralhadstha / zipcoder-laravel
1. Go to this page and download the library: Download pralhadstha/zipcoder-laravel 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/ */
$results = Zipcoder::lookup(Query::create('10001', 'US'));
if ($results->isEmpty()) {
// No addresses found
}
echo $results->count(); // Number of addresses returned
$array = $results->first()->toArray();
// or all results
$allArrays = $results->toArray();
namespace App\Zipcoder;
use Pralhad\Zipcoder\Contract\Provider;
use Pralhad\Zipcoder\Query;
use Pralhad\Zipcoder\Result\AddressCollection;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
class MyProvider implements Provider
{
public function __construct(
private ClientInterface $client,
private RequestFactoryInterface $requestFactory,
private string $api_url,
) {}
public function lookup(Query $query): AddressCollection
{
// Your implementation
}
public function getName(): string
{
return 'my-provider';
}
}
// Look up addresses for a postal code
Zipcoder::lookup(Query $query): AddressCollection
// Use a specific provider (bypasses chain)
Zipcoder::using(string $providerName): Provider
// Register an additional provider at runtime
Zipcoder::registerProvider(Provider $provider): ZipcoderLookup
// List all registered provider names
Zipcoder::getRegisteredProviders(): array