Repository navigation
Expand file tree
/
Copy pathsample-v4.php
More file actions
75 lines (58 loc) · 2.48 KB
/
Copy pathsample-v4.php
File metadata and controls
75 lines (58 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
require 'vendor/autoload.php';
use \GuzzleHttp\Client;
# An api key is emailed to you when you sign up to a plan
# Get a free API key at https:#api.the-odds-api.com/
$apiKey = $argv[1] ?? 'YOUR_API_KEY';
$sport = 'upcoming'; # use the sport_key from the /sports endpoint below, or use 'upcoming' to see the next 8 games across all sports
$regions = 'us'; # uk | us | eu | au. Multiple can be specified if comma delimited
$markets = 'h2h,spreads'; # h2h | spreads | totals. Multiple can be specified if comma delimited
$oddsFormat = 'decimal'; # decimal | american
$dateFormat = 'iso'; # iso | unix
$client = new Client();
# First get a list of in-season sports
# The sport 'key' from the response can be used to get odds in the next request
$sportsResponse = $client->request('GET', 'https://api.the-odds-api.com/v4/sports', [
'query' => ['api_key' => $apiKey],
'http_errors' => false,
]);
if ($sportsResponse->getStatusCode() !== 200) {
echo PHP_EOL;
echo "Failed to get sports: status_code {$sportsResponse->getStatusCode()}, response body {$sportsResponse->getBody()}";
echo PHP_EOL;
} else {
echo "List of in season sports: {$sportsResponse->getBody()}";
echo PHP_EOL;
}
# Now get a list of live & upcoming games for the sport you want, along with odds for different bookmakers
# This will deduct from the usage quota
# The usage quota cost = [number of markets specified] x [number of regions specified]
# For examples of usage quota costs, see https:#the-odds-api.com/liveapi/guides/v4/#usage-quota-costs
$oddsResponse = $client->request('GET', "https://api.the-odds-api.com/v4/sports/$sport/odds", [
'query' => [
'api_key' => $apiKey,
'regions' => $regions,
'markets' => $markets,
'oddsFormat' => $oddsFormat,
'dateFormat' => $dateFormat,
],
'http_errors' => false,
]);
if ($oddsResponse->getStatusCode() !== 200) {
echo PHP_EOL;
echo "Failed to get odds: status_code {$oddsResponse->getStatusCode()}, response body {$oddsResponse->getBody()}";
echo PHP_EOL;
} else {
$oddsJson = json_decode($oddsResponse->getBody());
echo PHP_EOL;
echo "Number of events: " . count( $oddsJson );
echo PHP_EOL;
echo $oddsResponse->getBody();
echo PHP_EOL;
# Check the usage quota
echo PHP_EOL;
echo "Remaining requests: " . $oddsResponse->getHeader('x-requests-remaining')[0];
echo PHP_EOL;
echo "Used requests: " . $oddsResponse->getHeader('x-requests-used')[0];
echo PHP_EOL;
}