-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrillboardsPartner.php
More file actions
182 lines (160 loc) · 5.69 KB
/
TrillboardsPartner.php
File metadata and controls
182 lines (160 loc) · 5.69 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/**
* Trillboards Partner API - PHP SDK
*
* Lightweight PHP client for Trillboards Partner API.
* Designed for server-to-server integrations (Tier 1).
*
* Usage:
* $client = new TrillboardsPartner('trb_partner_xxx');
* $config = $client->getVastConfig();
* // ... play ad ...
* $client->reportImpressions([...]);
*/
class TrillboardsPartner {
private string $apiKey;
private string $baseUrl;
private int $timeout;
public function __construct(
string $apiKey,
string $baseUrl = 'https://api.trillboards.com',
int $timeout = 30
) {
$this->apiKey = $apiKey;
$this->baseUrl = rtrim($baseUrl, '/');
$this->timeout = $timeout;
}
/**
* Get VAST configuration.
* Cache this response for 1 hour to reduce API calls.
*
* @param string|null $deviceId Optional device ID for device-specific config
* @return array API response with VAST tag URL template
* @throws Exception On API error
*/
public function getVastConfig(?string $deviceId = null): array {
$query = $deviceId ? "?device_id=" . urlencode($deviceId) : "";
return $this->request('GET', "/v1/partner/vast/config{$query}");
}
/**
* Report batch impressions with cryptographic proofs.
*
* @param array $impressions Array of impression objects
* @return array API response with proofs
* @throws Exception On API error
*/
public function reportImpressions(array $impressions): array {
return $this->request('POST', '/v1/partner/tracking/batch', [
'impressions' => $impressions
]);
}
/**
* Register a new device.
*
* @param string $deviceId Unique device identifier
* @param array $data Optional device data (name, location, etc.)
* @return array API response with device details
* @throws Exception On API error
*/
public function registerDevice(string $deviceId, array $data = []): array {
return $this->request('POST', '/v1/partner/device', array_merge(
['device_id' => $deviceId],
$data
));
}
/**
* Get partner info and stats.
*
* @return array API response with partner details
* @throws Exception On API error
*/
public function getInfo(): array {
return $this->request('GET', '/v1/partner/info');
}
/**
* Rotate API key with 24-hour grace period.
* WARNING: Save the new key immediately - it's only shown once.
*
* @return array API response with new API key
* @throws Exception On API error
*/
public function rotateApiKey(): array {
return $this->request('POST', '/v1/partner/api-key/rotate');
}
/**
* Make HTTP request to Trillboards API.
*
* @param string $method HTTP method
* @param string $endpoint API endpoint
* @param array|null $body Request body for POST/PUT
* @return array Decoded JSON response
* @throws Exception On HTTP or API error
*/
private function request(string $method, string $endpoint, ?array $body = null): array {
$url = $this->baseUrl . $endpoint;
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $this->timeout,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $this->apiKey,
'Content-Type: application/json',
'Accept: application/json'
]
]);
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
if ($body !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
}
}
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
throw new Exception("HTTP request failed: {$error}");
}
$data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new Exception("Invalid JSON response: " . json_last_error_msg());
}
if ($httpCode >= 400) {
$message = $data['message'] ?? $data['error']['message'] ?? 'Unknown error';
throw new Exception("API error ({$httpCode}): {$message}");
}
return $data;
}
}
// ==========================================
// Quick Start Example
// ==========================================
if (php_sapi_name() === 'cli' && basename(__FILE__) === basename($argv[0])) {
$apiKey = getenv('TRILLBOARDS_API_KEY');
if (!$apiKey) {
echo "Error: Set TRILLBOARDS_API_KEY environment variable\n";
exit(1);
}
$client = new TrillboardsPartner($apiKey);
echo "1. Fetching VAST configuration...\n";
$config = $client->getVastConfig();
echo " VAST URL: " . ($config['data']['vast_tag_template'] ?? 'N/A') . "\n\n";
echo "2. Your player fetches VAST directly from Google\n";
echo " (Trillboards not in critical ad serving path)\n\n";
echo "3. Reporting impression after ad completion...\n";
$result = $client->reportImpressions([
[
'device_id' => 'demo-device-001',
'ad_id' => 'ima_demo_ad_123',
'event' => 'complete',
'timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
'duration_ms' => 15000
]
]);
$processed = $result['data']['processed'] ?? 0;
$proofs = count($result['data']['proofs'] ?? []);
echo " Processed: {$processed}\n";
echo " Proofs: {$proofs} Ed25519 signatures\n\n";
echo "Integration complete! Your screens are now monetized.\n";
}