Statistics › Which trainers win at a course
Which trainers win at a course
The trainers with the most runners at Chester in the last five years, and how each has done.
<?php
// Which trainers win at a course
const BASE = "https://api.apihorseracing.com";
function get(string $path, array $params = []): array
{
$url = BASE . $path . ($params ? "?" . http_build_query($params) : "");
$ctx = stream_context_create(["http" => [
"header" => "X-API-Key: " . getenv("AHR_KEY"),
"ignore_errors" => true,
]]);
$body = json_decode(file_get_contents($url, false, $ctx), true);
if (isset($body["error"])) {
throw new RuntimeException($body["error"]["code"] . ": " . $body["error"]["message"]);
}
return $body["data"];
}
$data = get("/v1/courses/cr_1823JHN/stats", ["by" => "trainer", "months" => "60", "limit" => "10"]);
$key = $data["by"];
foreach ($data[$key] as $g) {
$label = $g[$key];
$share = $g["runs"] ? 100 * $g["first"] / $g["runs"] : 0;
printf("%s %d runs %.1f%% A/E %s\n", $label, $g["runs"], $share, $g["a_e"] ?? "-");
}
What the reply holds
- trainer
- The trainer.
- runs, first
- Runners and winners at the course.
- a_e
- Actual against expected.
When it says no
401no_key The key is missing or mistyped. Every sample reads it from AHR_KEY.403plan_needed Your plan does not reach this. The reply names the plan that does.429rate_limited Too fast. Wait the seconds in Retry-After, then try again.