philkershaw/postcodesio

查询Postcodes.io UK邮编API的简单界面。

1.0.1 2016-03-07 12:25 UTC

This package is not auto-updated.

Last update: 2020-10-30 21:57:13 UTC


README

Latest Version on Packagist Software License

描述

使用GuzzleHttp Version 6查询Postcodes.io UK邮编API的简单接口。

安装

通过Composer

$ composer require PhilKershaw/postcodesio

使用

简单地导入PhilKershaw\PostcodesIO\Client类,创建实例并调用以下任何方法:

use PhilKershaw\PostcodesIO\Client;

$client = new Client;

// Will return a whole host of information pertaining to the given post code.
$response = $client->postcodeLookup('W1A 1AA');

// Array containing postcodes.
$response = $client->bulkPostcodeLookup(['W1A 1AA', 'M50 2EQ']);

// Associative array containing latitude/longitude coordinates.
$response = $client->latLongLookup([
    'longitude' => 0.629834723775309,
    'latitude'  => 51.7923246977375
]);

// Nested associative arrays containing latitude/longitude coordinates and optional radius and limit.
// Radius will limit the range of the search and limit will, well, limit the number of results
// for a particular latitude/longitude lookup.
$response = $client->bulkLatLongLookup([
    [
        "longitude" => 0.629834723775309,
        "latitude"  => 51.7923246977375
    ],
    [
        "longitude" => -2.49690382054704,
        "latitude"  => 53.5351312861402,
        "radius"    => 1000,
        "limit"     => 5
    ]
]);

// Literally returns a random postcode.
$response = $client->getRandomPostcode();

$response = $client->validatePostcode('W1A 1AA');

// Will complete a partial postcode. Not tested thoroughly.
$response = $client->autocompletePostcode('W1A');

// Will return the nearest postcodes to a given post code.
$response = $client->getNearestPostcodes('W1A 1AA');

// Will perform an outcode lookup and return details specific to the outcode.
// NB The outcode refers to the first portion of the postcode.
// For example 'W1A' is the outcode in 'W1A 1AA'.
$response = $client->outcodeLookup('W1A');

// Similar to the nearest postcodes lookup but just for outcodes.
$response = $client->getNearestOutcodes('W1A');

// Similar to the straight lat/long lookup but just for outcodes.
$response = $client->outcodeLatLongLookup([
    'longitude' => 0.629834723775309,
    'latitude'  => 51.7923246977375
]);

所有响应都返回一个实现Psr\Http\Message\ResponseInterfaceGuzzleHttp\Psr7\Response实例。因此,您可以简单地通过调用以下方式获取状态码:

$response->getStatusCode();

和头部信息

$response->getHeaders();

特定的头部信息

$response->getHeader('Content-Type');

响应体

$response->getBody();

注意:Postcodes.io的响应通常采取以下形式

{
    "status": 200,
    "result": {...}
}

但是,由于'status'重复了HTTP响应码,实际上getBody()将仅返回'result',例如

$postcode = json_decode($response->getBody())->postcode;
// OR
$latitude = json_decode($response->getBody())->latitude;

更多信息,请参阅描述中链接的GuzzleHttp文档。

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。