mwgg/iracing-php

PHP的iRacing数据API包装器

v1.6.0 2023-08-19 07:59 UTC

This package is auto-updated.

Last update: 2024-09-19 10:13:20 UTC


README

PHP的iRacing数据API包装器。API文档和更多信息可以在iRacing论坛上找到。

基本用法

使用composer安装

composer require mwgg/iracing-php

所有API服务和方法的名称与它们的API端点相匹配。

API文档摘录

"stats": {
    "member_summary": {
        "link": "https://members-ng.iracing.com/data/stats/member_summary",
        "parameters": {
            "cust_id": {
                "type": "number",
                "note": "Defaults to the authenticated member."
            }
        }
    }
}

iRacingPHP

use iRacingPHP\iRacing;
$iracing = new iRacing('your@login.com', 'iRacingPassword');
$summary = $iracing->stats->member_summary();

注意,在这种情况下,cust_id参数是可选的,因此可以省略。所有被API文档标记为必需的参数都必须作为单独的参数传递给API方法,可选参数被组合到一个单独的数组参数中。这使得处理具有大量可选参数的方法变得更加容易。

API文档摘录

"league": {
    "season_standings": {
        "link": "https://members-ng.iracing.com/data/league/season_standings",
        "parameters": {
            "league_id": {
                "type": "number",
                "required": true
            },
            "season_id": {
                "type": "number",
                "required": true
            },
            "car_class_id": {
                "type": "number"
            },
            "car_id": {
                "type": "number",
                "note": "If car_class_id is included then the standings are for the car in that car class, otherwise they are for the car across car classes."
            }
        }
    }
}

iRacingPHP

use iRacingPHP\iRacing;
$iracing = new iRacing('your@login.com', 'iRacingPassword');
$standings = $iracing->league->season_standings(12345, 54321, [
    'car_id' => 123
]);

速率限制

在发起请求后,可以检查当前的速率限制值。

$iracing->api->rateLimits->limit; // The current total rate limit
$iracing->api->rateLimits->remaining; // How much of the rate limit you have remaining
$iracing->api->rateLimits->reset; // When the rate limit will reset in epoch timestamp

请注意,这些速率限制值将在所有请求之后更新,除了 $iracing->constants->端点,因为这些是从本地检索的。

Cookie

iRacingPHP使用cookie在请求和执行之间持久化身份验证。默认情况下,cookie存储在/tmp/iracingphpcookie

如果您想更改cookie文件的位置,可以在实例化iRacing对象时进行更改。

$iracing = new iRacing('your@login.com', 'iRacingPassword', '/path/to/cookies');

只要在所有请求和执行中使用相同的路径,身份验证就会持续。

异常

iRacingPHP抛出以下异常

当尝试访问不存在的API服务时,会抛出iRacingPHP\Exceptions\ApiServiceNotFoundException异常。

当认证请求因任何原因失败时,会抛出iRacingPHP\Exceptions\AuthenticationRequestFailedException异常。

当认证HTTP请求成功但未发生认证(错误的用户名或密码等)时,会抛出iRacingPHP\Exceptions\AuthenticationFailedException异常。

当API端点请求失败时,会抛出iRacingPHP\Exceptions\RequestFailedException异常。

当请求获取缓存数据失败时,会抛出iRacingPHP\Exceptions\DataRequestFailedException异常。

当请求因速率限制而失败时,会抛出iRacingPHP\Exceptions\RequestRateLimitedException异常。

当iRacing服务因维护而关闭时,会抛出iRacingPHP\Exceptions\SiteMaintenanceException异常。

在所有情况下,您都可以使用$e->getMessage();查看错误消息。

在除iRacingPHP\Exceptions\ApiServiceNotFoundException异常之外的所有情况下,Guzzle异常将与iRacingPHP异常链在一起。