-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtractiveapi.php
More file actions
125 lines (113 loc) · 4.21 KB
/
tractiveapi.php
File metadata and controls
125 lines (113 loc) · 4.21 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//gus mueller, tractive API
$yourEmail = "your@email.com";
$yourPassword = "YOURPASSWORD";
$yourTrackerCode = "XXYYZZYY";
$baseUrl = "https://graph.tractive.com/4";
// URL to retrieve token
$url = $baseUrl . "/auth/token";
//google SSO would be like so, but google single sign in is a headache, so AVOID
//$url = $baseUrl . "/auth/token/google";
// Payload to POST
// this:
$data = [
"platform_email" => $yourEmail,
"platform_token" => $yourPassword,
"grant_type" => "tractive"
];
//not this, this would be for google SSO:
/*
$data =
[
"auth_code"=> "4/0AVMBsJjosQ71dTZn-LdRDPBBNIfJN6hP1pZ9IS5I4BCC2qGw-B3Vd15Y1MWWujBC0xOjfg",
"locale"=>"en_US",
"all_terms_accepted"=> true
]
;
*/
// Initialize cURL
$ch = curl_init($url);
$headers = [
'Accept: application/json, text/plain, */*',
'Content-Type: application/json;charset=UTF-8',
'Origin: https://my.tractive.com',
'Referer: https://my.tractive.com/',
'Sec-CH-UA: "Not)A;Brand";v="8", "Chromium";v="138", "Google Chrome";v="138"',
'Sec-CH-UA-Mobile: ?0',
'Sec-CH-UA-Platform: "Windows"',
'Sec-Fetch-Dest: empty',
'Sec-Fetch-Mode: cors',
'Sec-Fetch-Site: same-site',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36',
'X-Tractive-App: site.tractivegps',
'X-Tractive-AppBuild: 1c46d9f2',
'X-Tractive-AppVersion: 2025-08-12T11:12:32.025Z#1c46d9f2',
'X-Tractive-Client: 5728aa1fc9077f7c32000186',
'X-Tractive-User: unknown'
];
// Set cURL options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// Execute request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo "cURL error: " . curl_error($ch);
curl_close($ch);
exit;
}
// Close cURL
curl_close($ch);
// Decode JSON response
$result = json_decode($response, true);
// Check if decoding succeeded
if ($result === null) {
echo "Failed to decode JSON response";
exit;
}
// FOR DEBUGGING! Print the access token
/*
echo "Access Token: " . $result['access_token'] . PHP_EOL;
echo "Expires at: " . date('Y-m-d H:i:s', $result['expires_at']) . PHP_EOL;
echo "User ID: " . $result['user_id'] . PHP_EOL;
echo "Client ID: " . $result['client_id'] . PHP_EOL;
*/
$timestamp = time();
$timezoneOffset = 18000; //for east coast!
$url = $baseUrl . "/tracker/" . $yourTrackerCode . "/positions?time_from=" . intval(18000 + $timestamp - 36000) . "&time_to=" . intval(18000 + $timestamp) . "&format=json_segments";
echo $url;
$headers = [
"accept: application/json, text/plain, */*",
"accept-encoding: gzip, deflate, br, zstd",
"authorization: Bearer " . $result['access_token'],
"origin: https://my.tractive.com",
"referer: https://my.tractive.com/",
"sec-ch-ua: \"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Google Chrome\";v=\"138\"",
"sec-ch-ua-mobile: ?0",
"sec-ch-ua-platform: \"Windows\"",
"sec-fetch-dest: empty",
"sec-fetch-mode: cors",
"sec-fetch-site: same-site",
"user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36",
"x-tractive-app: site.tractivegps",
"x-tractive-appbuild: 1c46d9f2",
"x-tractive-appversion: 2025-08-12T11:12:32.025Z#1c46d9f2",
"x-tractive-client: " . $result['client_id'],
"x-tractive-user: " . $result['user_id']
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_ENCODING, ""); // allow gzip/br decoding
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo "cURL error: " . curl_error($ch);
}
curl_close($ch);
//DEBUGGING Output raw response
echo $response;
//what comes back from tractive is an array of objects, each of this form:
//{time, latlong, alt, speed, pos_uncertainty, sensor_used}...