godmodelabs/flora-client-php

Flora 基础 API 的 PHP 客户端

0.12.0 2024-04-02 12:49 UTC

This package is auto-updated.

Last update: 2024-09-24 12:07:14 UTC


README

轻松访问基于 Flora 的 API。

$client = new \Flora\Client('http://api.example.com/');
$response = $client->execute([
    'resource'  => 'foo',
    'select'    => 'id,name'
]);

原始响应

返回 PSR-7 响应对象(例如处理二进制数据)。

$client = new \Flora\Client('http://api.example.com/');
$response = $client->executeRaw([
    'resource' => 'article',
    'id' => 1337,
    'action' => 'pdf',
]);

异步请求(使用 guzzlehttp/promises

use GuzzleHttp\Promise;

$client = new \Flora\Client('http://api.example.com/');
try {
    $fooPromise = $client->executeAsync([
        'resource' => 'foo',
        'select' => 'id,name'
    ]);
    $barPromise = $client->executeAsync([
        'resource' => 'bar',
        'select' => 'id,name'
    ]);
    
    [$fooResponse, $barResponse] = Promise\Utils::unwrap([$fooPromise, $barPromise]);
    // process responses...
} catch (Throwable $e) {
    echo $e->getMessage(), PHP_EOL;
}

并行请求

简单的接口,用于同时执行多个 API 请求。基本上隐藏了上述示例的复杂性。

$client = new \Flora\Client('http://api.example.com/');
try {
    [$fooResponse, $barResponse] = $client->executeParallel([
        ['resource' => 'foo', 'select' => 'id,name'],
        ['resource' => 'bar', 'select' => 'id,name']
    ]);
    // process responses...
} catch (Throwable $e) {
    echo $e->getMessage(), PHP_EOL;
}