merlindiavova/postcodesio

0.1.0 2019-06-20 21:45 UTC

This package is auto-updated.

Last update: 2024-09-21 20:43:47 UTC


README

Build Status Build Status Latest Stable Version License

PHP 客户端,用于调用 Postcodes.io API。

安装

建议使用 Composer 来安装此库。当前支持的 PHP 版本是 7.1-7.3。

要安装此库,请运行以下命令

$ composer require merlindiavova/postcodesio

示例

下面的示例使用 sunrise/http-client-curl 作为 PSR-18 实现,并使用 Nyholm/psr7 作为 PSR-7 实现。

<?php

declare(strict_types=1);

use PostcodesIO\API\Client;
use PostcodesIO\API\Client\ResponseCheck;
use PostcodesIO\API\Factory\Psr17Factory;
use Sunrise\Http\Client\Curl\Client as SunClient;
use Nyholm\Psr7\Factory\Psr17Factory as NyholmPsr17Factory;

require __DIR__ . '/vendor/autoload.php';

$psr7Implementation = new NyholmPsr17Factory();

$postcodesIoClient = new Client(
    new SunClient($psr7Implementation),
    new Psr17Factory(
        $psr7Implementation,
        $psr7Implementation,
        $psr7Implementation,
        $psr7Implementation
    )
);

$response = $postcodesIoClient->getPostcodeClient()->fetch('NW10 4DG');

$postcode = $response->getFirstResult(); // PostcodesIO\API\Postcode\Data

echo $postcode->getAdminWard() . ', ' . $postcode->getAdminDistrict(); // Harlesden, Brent

选择 PSR-7 实现

此库不包含任何 PSR-7 实现。您可以根据自己的情况自由选择任何实现。以下是一些值得注意的实现:

  • Slim-Psr7 - 使用 composer require slim/psr7 安装。这是 Slim 框架项目的 PSR-7 实现。
  • Nyholm/psr7 - 使用 composer require nyholm/psr7 安装。这是目前最快、最严格且最轻量级的实现。
  • Guzzle/psr7 - 使用 composer require guzzlehttp/psr7 安装。这是 Guzzle 客户端使用的实现。它不是最严格的,但为流和文件处理添加了一些很好的功能。它是第二快的实现,但稍微大一些。
  • zend-diactoros - 使用 composer require zendframework/zend-diactoros 安装。这是 Zend 的实现。这是四种实现中最慢的一种。

已选择的 psr7-implementations 取自 https://github.com/slimphp/Slim/tree/4.x

选择 PSR-18 实现

此包不包含 PSR-18 HTTP 客户端实现。您可以根据自己的情况自由选择任何实现。以下是一些选项:

  • sunrise/http-client-curl - 使用 composer require sunrise/http-client-curl 安装。超级轻量级 Curl 客户端。
  • kriswallsmith/buzz - 使用 composer require kriswallsmith/buzz 安装。另一个轻量级客户端。
  • php-http/guzzle6-adapter - 使用 composer require php-http/guzzle6-adapter 安装。笨重的 Guzzle 6 HTTP 适配器。

API 客户端

Postcodes

<?php
// ... 
// Returns the postcode api client
$postcodesClient = $postcodesIoClient->getPostcodeClient();

// Fetch a postcode
$response = $postcodesClient->fetch('HA0 2TF'); // Returns a Postcode\Response object
$postcode = $response->getFirstResult(); // Returns Postcode\Data object

// Fetch many postcodes
$response = $postcodesClient->fetchByBulk(['HA0 2TF', 'SE1 2UP']); // Returns a Postcode\Response object
$postcodes = $response->getResult(); // Returns Client\Collection of Postcode\Data objects

// Fetch Reverse Geocode
$response = $postcodesClient->fetchByReverseGeocode(-0.076579, 51.503378); // Returns a Postcode\Response object
$postcodes = $response->getResult(); // Returns Client\Collection of Postcode\Data objects

// you add an array of optional query parameters as the third argument
$queryParams = ['limit' => 10, 'radius' => '1000', 'wideSearch' => true];
$response = $postcodesClient->fetchByReverseGeocode(-0.076579, 51.503378, $queryParams);