fw4/realo-api-client

Realo API 客户端

v1.0.3 2024-04-23 06:39 UTC

This package is auto-updated.

Last update: 2024-09-23 08:15:09 UTC


README

这是一个用于使用 Realo REST API 的 PHP 库。

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

安装

作为 composer 依赖项(推荐)

安装 Realo PHP API 客户端的首选方式是通过 composer

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

composer require fw4/realo-api-client

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

require_once __DIR__ . '/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