nkl-kst/the-sports-db

PHP 库,用于从 TheSportsDB (https://www.thesportsdb.com) 获取体育数据

1.5.6 2024-09-23 04:12 UTC

README

Build Packagist Version Coverage Status PHPStan Level GitHub

一个易于使用的 PHP 库,用于从 https://www.thesportsdb.com 获取数据。

特性

  • 获取列表、实时比分、查找、日程安排、搜索或视频精彩片段的数据
  • 以序列化类形式获取结果
  • 使用您自己的 API 密钥
  • 限制长时间运行的脚本
  • 使用 PSR-4 自动加载
  • 支持 PHP 7.4+

此库使用 语义化版本控制

安装

使用 Composer 安装此 PHP 库。

> composer require nkl-kst/the-sports-db

用法

获取数据

从 TheSportsDB 获取体育数据。

// You need to load the Composer autoload file somewhere in your code before
require_once 'vendor/autoload.php';

use NklKst\TheSportsDb\Client\ClientFactory;

// Create a client
$client = ClientFactory::create();

// Get soccer livescores
$livescores = $client->livescore()->now('Soccer');
echo $livescores[0]->strProgress;

// Get video highlights
$highlights = $client->highlight()->latest();
echo $highlights[0]->strVideo;

// Get next events for Liverpool FC
$events = $client->schedule()->teamNext(133602);
echo $events[0]->strEvent;

查看 集成测试,了解所有 已记录的 API 调用 的示例。

使用您的 API 密钥

使用您自己的 Patreon API 密钥。

use NklKst\TheSportsDb\Client\ClientFactory;

// Set an API key
$client = ClientFactory::create();
$client->configure()->setKey('YOUR_API_KEY');

限制请求

建议您不要在每分钟内对 TheSportsDB API 进行超过 100 次请求(硬限制为每秒两次请求)。如果您有需要收集大量数据的长时间运行的脚本,请考虑使用内置的速率限制器。

use NklKst\TheSportsDb\Client\ClientFactory;

// Use the default rate limit of 100 requests per minute
$client = ClientFactory::create();
$client->configure()->setRateLimiter();

// Do your requests as usual

// You can unset the rate limiter later
$client->configure()->unsetRateLimiter();

由于此库使用 Symfony Rate Limiter 组件,因此可以使用自定义速率限制机制。有关更多信息,请参阅 Symfony 文档

已知问题

  • 不支持 v1 的实时比分。
  • 如果您遇到类似于 JSON 属性 "foo" 在类 "Bar" 中不得为 NULL 的异常,那么有一个实体属性应该是可空的。在这种情况下,请创建一个新的问题。

反馈

如果您有任何问题或疑问,请随时创建问题或拉取请求。

开发者备注

运行测试和代码检查。

# Unit tests
> composer test-unit

# Integration tests (API calls, Patreon key required)
> PATREON_KEY=<YOUR_PATREON_KEY> composer test-integration
# On Windows use 'set PATREON_KEY=<YOUR_PATERON_KEY>' before running the tests

# Analyze code (static analysis)
> composer analyze-code

# Check code (coding standards)
> composer check-code

许可证:MIT

查看 LICENSE