baldwin/medipim-api-client

Medipim API (v3 及以上版本) 的 PHP 客户端

2.0.1 2023-11-08 08:58 UTC

This package is auto-updated.

Last update: 2024-09-08 11:31:12 UTC


README

这个库帮助您连接并使用 Medipim API (v3 及以上版本)。

您不必使用此库来调用 API,但如果您使用 php,它应该很有帮助。

比利时

法国

功能

  • 处理身份验证,只需提供您的 API 密钥和密钥。
  • 负责编码请求和解码结果。
  • 限制您的请求,并在超出最大请求速率时自动重试请求。
  • 将错误优雅地转换为异常。

安装

使用 composer

$ composer require baldwin/medipim-api-client

基本用法

使用您的 API 凭据实例化客户端类。使用 getpoststream 来发送请求。

<?php

use Medipim\Api\Client;

require "vendor/autoload.php";

$apiKeyId = ...;
$apiKeySecret = ...;
$client = new Client($apiKeyId, $apiKeySecret, "https://api.medipim.be");

$products = $client->post("/v4/products/query", [
    "filter" => ["updatedSince" => time() - 24*60*60]
]);

foreach ($products["results"] as $product) {
    // ...
}

API

客户端

__construct(int $apiKeyId, string $apiKeySecret, string $baseUrl)

创建一个新的客户端。

get(string $path, array $query): array

执行 GET 请求。

<?php
$r = $client->get("/v4/products/find?id=xxx");
var_dump($r); // array(2) { "product" => ... }

post(string $path, array $body): array|\Iterator

执行带有 json 体的简单 POST 请求。

注意,一些 POST 端点会流式传输其响应,在这种情况下,此函数返回一个结果集的 \Iterator

<?php
$r = $client->post("/v4/products/query", ["filter" => ["updatedSince" => strtotime("yesterday")]]);
var_dump($r); // array(2) { ["meta"] => ..., ["results"] => ... }

stream($path, array $query = [], callable $callback = null)

此方法仅应用于 /stream 端点。

定义一个可调用来处理数据。

MedipimApiError

继承自 \RuntimeException

API 错误被转换为此类的实例并抛出。

您可以使用此对象如数组一样访问错误的任何属性。

<?php
try {
    $r = $client->get("/v4/foo/bar");
    // responds with {"error": {"code": "endpoint_not_found", ...}}
} catch (MedipimApiError $e) {
    var_dump($e["code"]); // string(18) "endpoint_not_found"
}