vladshut / postcode-api-client
postcodeapi.nu网络服务的客户端库。
1.0
2019-05-11 08:15 UTC
Requires
- php: >=5.4.0
- ext-json: *
- guzzlehttp/psr7: ^1.3
- php-http/httplug: ^1.1
Requires (Dev)
- php-http/mock-client: ^0.3.0
- phpunit/phpunit: ^4.3.5|^5.0
Suggests
- php-http/guzzle6-adapter: An HTTPlug adapter for the Guzzle 6 HTTP client
This package is auto-updated.
Last update: 2024-09-11 20:06:38 UTC
README
PostcodeAPIClient 是 PostcodeAPI.nu 网络服务的 PHP 客户端库。
需求
PostcodeAPIClient 与 PHP 5.5.0 或更高版本兼容。此库依赖于 HTTPPlug,请参阅 http://docs.php-http.org/en/latest/httplug/introduction.html。
安装
可以使用 Composer 轻松安装 PostcodeAPIClient。
composer require vladshut/postcode-api-client
使用方法
实例化客户端,并将 API 密钥替换为您的个人凭据
// Use the composer autoloader to load dependencies require_once 'vendor/autoload.php'; // initiate client $apiKey = 'replace_with_your_own_api_key'; // In this example we made use of the Guzzle6 as HTTPClient in combination with an HTTPPlug compatible adapter. $client = new \VS\PostcodeAPI\Client( new Http\Adapter\Guzzle6\Client( new GuzzleHttp\Client([ 'headers' => [ 'X-Api-Key' => $apiKey ] ]) ) ); // call endpoints $response = $client->getAddresses('5041EB', 21); $response = $client->getAddress('0855200000061001'); $response = $client->getPostcode('5041EB'); // Note that this call is only available with a premium account $response = $client->getPostcodes('51.566405', '5.077171');
请注意,要能够运行上面的示例,您应该已经运行了以下命令,以便有 Guzzle6 和适配器可用。
composer require php-http/guzzle6-adapter
在 Symfony 项目中
我们建议使用 Guzzle,要能够将 Guzzle 与 PostcodeApiClient 结合使用,您还应该使用 Guzzle6Adapter。运行以下命令可以自动安装 Guzzle。
composer require php-http/guzzle6-adapter
并添加以下服务定义
project.http.guzzle.client: class: GuzzleHttp\Client arguments: - { headers: { X-Api-Key: 'replace_with_your_own_api_key' } } project.http.adapter.guzzle.client: class: Http\Adapter\Guzzle6\Client arguments: - '@project.http.guzzle.client' project.client.postal_code: class: VS\PostcodeAPI\Client arguments: - '@project.http.adapter.guzzle.client'
现在,您应该能够使用 project.client.postal_code
服务向 PostcodeAPI 发送请求。