Statistics › Favourites on each going
Favourites on each going
How often favourites have won on each going in Great Britain over five years, from the whole archive in under a second.
<?php
// Favourites on each going
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/analysis/breakdown", ["favourite" => "1", "by" => "going", "region" => "GB", "months" => "60"]);
$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
- overall
- The whole group together: the average to compare against.
- going
- One row per going.
- 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.