baldwin / medipim-api-v3-client
0.6
2021-10-25 09:57 UTC
Requires
- php: ^5.6 || ^7.0 || ^8.0
- ext-curl: *
- ext-json: *
This package is auto-updated.
Last update: 2022-07-14 06:18:13 UTC
README
此库可以帮助您连接并使用 Medipim API(v3)。
您不需要使用此库来调用 API,但如果您使用 php,则它应该很有帮助。
- Medipim 平台:https://platform.medipim.be
- Medipim API v3 文档:https://platform.medipim.be/docs/api/v3/
- 支持:support@medipim.be
功能
- 处理身份验证,只需提供您的 API 密钥和密钥。
- 处理请求编码和结果解码。
- 限制您的请求,并在您超过最大请求速率时自动重试请求。
- 将错误优雅地转换为异常。
安装
使用 composer
$ composer require baldwin/medipim-api-v3-client
基本用法
使用您的 API 凭据实例化客户端类。使用 get
、post
或 stream
发送请求。
<?php
use Medipim\Api\V3\MedipimApiV3Client;
require "vendor/autoload.php";
$apiKeyId = ...;
$apiKeySecret = ...;
$client = new MedipimApiV3Client($apiKeyId, $apiKeySecret);
$products = $client->post("/v3/products/search", [
"filter" => ["updatedSince" => time() - 24*60*60]
]);
foreach ($products["results"] as $product) {
// ...
}
API
MedipimApiV3Client
__construct(int $apiKeyId, string $apiKeySecret)
创建一个新的客户端。
get(string $path, array $query): array
执行 GET 请求。
<?php
$r = $client->get("/v3/apb-categories/all");
var_dump($r); // array(2) { ["meta"] => ..., ["results"] => ... }
post(string $path, array $body): array|\Iterator
执行带有 json 体的简单 POST 请求。
请注意,一些 POST 端点会流式传输其响应,在这种情况下,此函数返回一个 \Iterator
,它遍历结果。
<?php
$r = $client->post("/v3/products/search", ["filter" => ["updatedSince" => strtotime("yesterday")]]);
var_dump($r); // array(2) { ["meta"] => ..., ["results"] => ... }
stream($path, array $query = [], callable $callback)
此方法仅用于 /export 端点。
应定义一个可调用来处理数据。
setBaseUrl(string $url): void
默认情况下,客户端连接到 'https://api.medipim.be'。如果您想使用沙盒服务器上的客户端,可以使用此方法。
<?php
$client->setBaseUrl("https://api.sandbox.medipim.be");
请注意,沙盒凭据与我们的主服务器分开。沙盒产品数据会定期清除。
MedipimApiV3Error
extends \RuntimeException
.
API 错误被转换为此类的实例并抛出。
您可以使用此对象像数组一样访问错误的任何属性。
<?php
try {
$r = $client->get("/v3/foo/bar");
// responds with {"error": {"code": "endpoint_not_found", ...}}
} catch (MedipimApiV3Error $e) {
var_dump($e["code"]); // string(18) "endpoint_not_found"
}