flashmatt/postcodesio

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

1.3 2018-03-26 14:00 UTC

This package is not auto-updated.

Last update: 2024-09-18 09:32:14 UTC


README

Software License

描述

使用GuzzleHttp版本6,为查询Postcodes.io英国邮编API提供简单接口。

安装

通过Composer

$ composer require PhilKershaw/postcodesio

用法

简单地导入flashmatt\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)。请参阅许可证文件以获取更多信息。