sportrizer/sportysky-client-php

Sportysky PHP 客户端

2.1.9 2020-05-07 15:01 UTC

README

SportySKY PHP 客户端

如何在您的 PHP 项目中集成 SportySKY

Build Status codecov

要求

入门

使用 composer 包管理器安装 SportySKY PHP 客户端

composer require sportrizer/sportysky-client-php

API 客户端

getCountryForecastResponse

示例

$response = $apiClient->getCountryForecastResponse('FR', new \DateTime('2020-01-14T17:36:00+00:00'));
$data = json_decode($response->getBody()->getContents(), true);

getRegionForecastResponse

示例

$response = $apiClient->getRegionForecastResponse('FR-BRE', new \DateTime('2020-01-14T17:36:00+00:00'));
$data = json_decode($response->getBody()->getContents(), true);

getDepartmentForecastResponse

示例

$response = $apiClient->getDepartmentForecastResponse('FR-29', new \DateTime('2020-01-14T17:36:00+00:00'));
$data = json_decode($response->getBody()->getContents(), true);

getSpotForecastResponse

示例

$response = $apiClient->getSpotForecastResponse(
    '1234-1234-1234-1234',
    new \DateTime('2020-01-14T17:36:00+00:00'),
    new \DateTime('2020-01-14T17:36:00+00:00')
);
$data = json_decode($response->getBody()->getContents(), true);

getSpotForecastByCodeAndCountryResponse

示例

$response = $apiClient->getSpotForecastByCodeAndCountryResponse(
    '29000',
    'FR',
    new \DateTime('2020-01-14T17:36:00+00:00'),
    new \DateTime('2020-01-14T17:36:00+00:00')
);
$data = json_decode($response->getBody()->getContents(), true);

getSpotsResponse

示例

use Sportrizer\Sportysky\Utils\Geo\Box;
use Sportrizer\Sportysky\Utils\Geo\Point;

// search nearest spot
$response = $apiClient->getSpotsResponse(
    new Point(33.3, 44.4)
);
$data = json_decode($response->getBody()->getContents(), true);

// find all spots inside a bounding box
$response = $apiClient->getSpotsResponse(
    null,
    new Box(new Point(22.2, 33.3), new Point(33.3, 44.4))
);

// pagination
$response = $apiClient->getSpotsResponse(
    null,
    null,
    3 // get page 3
);

$data = json_decode($response->getBody()->getContents(), true);

getForecastResponse

示例

$response = $apiClient->getForecastResponse(new \DateTime('2020-01-14T17:36:00+00:00'), null, null, null, 'FR');
$data = json_decode($response->getBody()->getContents(), true);

与 SportySKY JavaScript 库的集成

此库开发用于与 SportRIZER 提供的 SportySKY JavaScript 库无缝工作。

创建一个将被 JavaScript 库调用的 php 脚本

<?php

declare(strict_types=1);

use GuzzleHttp\Psr7\ServerRequest;
use Sportrizer\Sportysky\ApiClient;
use Sportrizer\Sportysky\Authenticator;
use Sportrizer\Sportysky\ServerRequestHandler;
use Laminas\HttpHandlerRunner\Emitter\SapiStreamEmitter;

require '../vendor/autoload.php';

// Authenticate the server to SportRIZER
$authenticator = new Authenticator(getenv('SPORTYSKY_CLIENT_ID'), getenv('SPORTYSKY_CLIENT_SECRET'));

// Create a new SportySKY API client
// with the JWT token provided by the authenticator
$apiClient = new ApiClient($authenticator->getToken());

// Handles the request made by the JS API
$apiResponse = (new ServerRequestHandler($apiClient))->handle(ServerRequest::fromGlobals());

// Outputs the SportySKY API response
(new SapiStreamEmitter())->emit($apiResponse);

注意:SapiStreamEmitter 随库附带一个建议的库。
使用此命令安装它

composer require laminas/laminas-httphandlerrunner

您应在环境变量中设置您的客户端 ID(SPORTYSKY_CLIENT_ID)和客户端密钥(SPORTYSKY_CLIENT_SECRET)。

此脚本将验证您的服务器,并从 SportySKY API 返回 JSON,该 JSON 将被 JavaScript 库消费。

缓存

SportySKY API 响应

API 调用通过 Guzzle 进行,可以配置 Kevinrob 的缓存中间件

例如,您可以为 ApiClient 的第二个参数提供一个 PSR-16 兼容的 Redis 缓存。

use Desarrolla2\Cache\Predis;
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Storage\Psr16CacheStorage;
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
use Predis\Client;
use Sportrizer\Sportysky\ApiClient;

// Authenticator ... 

$cacheHandler = HandlerStack::create();
$cacheHandler->push(new CacheMiddleware(
    new PublicCacheStrategy(
        new Psr16CacheStorage(
            new Predis(
                new Client(getenv('REDIS_URL')) // tcp://127.0.0.1:6379
            )
        )
    )
));

$apiClient = new ApiClient($authenticator->getToken(), $cacheHandler);

注意:需要 predis/predis 包来使 Redis 缓存工作。
使用此命令安装它

composer require predis/predis

查看完整示例

默认情况下,响应将根据 API 提供的缓存头进行缓存,但您也可以定义自己的策略:查看更多示例

此库已附带一些其他 PSR-16 适配器

https://github.com/desarrolla2/Cache#adapters

JWT 认证令牌

默认情况下,JWT 令牌将缓存在临时系统目录中,直到其过期,但您可以将自己的 PSR-16 缓存集成作为 Authenticator 的第三个参数提供。

Redis 示例

use Desarrolla2\Cache\Predis;
use Predis\Client;
use Sportrizer\Sportysky\Authenticator;

$redisCache = new Predis(new Client(getenv('REDIS_URL'))); // tcp://127.0.0.1:6379

$authenticator = new Authenticator(getenv('SPORTYSKY_CLIENT_ID'), getenv('SPORTYSKY_CLIENT_SECRET'), $redisCache);

修改

API 返回修改

API 返回可以在处理或缓存之前进行修改

use Sportrizer\Sportysky\ApiClient;
use Sportrizer\Sportysky\Authenticator;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\ResponseInterface;

$authenticator = new Authenticator(getenv('SPORTYSKY_CLIENT_ID'), getenv('SPORTYSKY_CLIENT_SECRET'));

$handler = HandlerStack::create();
$handler->push(
    Middleware::mapResponse(
        function (ResponseInterface $response) {
            $bodyContent                    = json_decode($response->getBody()->getContents(), true);
            $bodyContent['test_Middleware'] = true;

            return $response->withBody(
                \GuzzleHttp\Psr7\stream_for(
                    json_encode($bodyContent)
                )
            );
        }
    )
);

$apiClient = new ApiClient($authenticator->getToken(), $handler);

API 返回和缓存修改

在 /tmp 上的缓存文件示例

use Sportrizer\Sportysky\ApiClient;
use Sportrizer\Sportysky\Authenticator;
use GuzzleHttp\HandlerStack;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Middleware;
use Kevinrob\GuzzleCache\CacheMiddleware;
use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy;
use Doctrine\Common\Cache\FilesystemCache;
use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage;

$authenticator = new Authenticator(getenv('SPORTYSKY_CLIENT_ID'), getenv('SPORTYSKY_CLIENT_SECRET'));

$handler = HandlerStack::create();
$handler->push(
    Middleware::mapResponse(
        function (ResponseInterface $response) {
            $bodyContent                    = json_decode($response->getBody()->getContents(), true);
            $bodyContent['test_Middleware'] = true;

            return $response->withBody(
                \GuzzleHttp\Psr7\stream_for(
                    json_encode($bodyContent)
                )
            );
        }
    )
);

$handler->push(
    new CacheMiddleware(
        new PublicCacheStrategy(
            new DoctrineCacheStorage(
                new FilesystemCache('/tmp/')
            )
        )
    )
);

$apiClient = new ApiClient($authenticator->getToken(), $handler);

示例

与 JS 库的集成

测试

composer test