realo/api-client

官方Realo API客户端

v1.0.3 2019-01-09 14:27 UTC

This package is auto-updated.

Last update: 2024-09-10 04:50:30 UTC


README

使用Realo REST API的官方PHP库。

在使用此库之前,您必须拥有有效的API密钥。要获取API密钥,请联系您的Realo客户成功经理。

安装

作为composer依赖(推荐)

安装Realo PHP API客户端的推荐方式是通过composer

接下来,运行composer命令安装Realo PHP API客户端

composer require realo/api-client

安装后,您需要引入Composer的自动加载器

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

作为Phar

您可以从我们的发布下载一个可用的Realo API客户端库Phar版本。这包括API客户端及其所有依赖项。

下载后,您需要引入捆绑的自动加载器

require_once 'phar://' . __DIR__ . '/realo-api-client.phar/vendor/autoload.php';

初始化

RealoApi::create(publicKey, privateKey, environment)

  • publicKey
    • 类型: string
    • API公共密钥
  • privateKey
    • 类型: string
    • API私有密钥
  • environment
    • 类型: string
    • 默认值: production
    • API环境(可以是productionsandbox

RealoApi::createWithClient(publicKey, privateKey, client)

  • publicKey
    • 类型: string
    • API公共密钥
  • privateKey
    • 类型: string
    • API私有密钥
  • client
    • 类型: GuzzleHttp\Client
    • Guzzle HTTP客户端,用于与REST API通信

方法

request(path, method, [, payload [, headers]])

  • path
    • 类型: string
    • 资源的路径
  • method
    • 类型: string
    • 请求的HTTP方法
  • payload
    • 类型: array
    • HTTP有效负载(将编码为JSON),仅在HTTP方法不是GET时适用
  • headers
    • 类型: array
    • 要随请求一起发送的自定义头

示例

使用捆绑的CLI工具

我们提供了一个简单的CLI实用工具,您可以用来与我们的API交互。

$ php example/simple-cli.php --public-key=xxx --private-key=xxx /valuations/xxx/data/mobility
{
    "data": {
        "mobilityScore": 0.88,
        "distanceToCityCenter": 3130,
        "distanceToBusStop": 323,
        "distanceToTrainStation": 1197,
        "distanceToSchool": 77,
        "distanceToStores": 33,
        "distanceToHighways": 3282,
        "buildingDensity": 1688.01,
        "inhabitantsDensity": 32346.18,
        "transitTypeCityCenter": "cycling-distance",
        "transitTypeBusStop": "walking-distance",
        "transitTypeTrainStation": "walking-distance",
        "transitTypeSchool": "walking-distance",
        "transitTypeStores": "walking-distance"
    }
}

使用请求函数发送API调用

我们提供了一个基本请求函数来访问我们的任何API资源。

use Realo\Api\RealoApi;
use Realo\Api\RealoApiException;

$api = RealoApi::create('YOUR_PUBLIC_KEY', 'YOUR_PRIVATE_KEY');
try {
    $response = $api->request('/agencies', 'GET');
    var_dump($response);
} catch (RealoApiException $e) {
    printf("Error %d: %s\n", $e->getCode(), $e->getMessage());
}

异常

有两种情况下会抛出异常:请求有问题或服务器返回状态码为400或更高。

RealoApiException

  • getCode()
    • 返回响应状态码为400或更高的值。
  • getMessage()
    • 返回异常消息。
  • getErrors()
    • 如果响应体包含一个包含错误array的响应,则返回这些错误。否则返回null
  • getType()
    • 如果响应体包含错误,则返回其类型。否则返回null