-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_webhook.php
83 lines (76 loc) · 2.41 KB
/
example_webhook.php
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
<?php
ini_set('display_errors', 1);
include './vendor/autoload.php';
use Adquesto\SDK\Content;
use Adquesto\SDK\InMemoryStorage;
use Adquesto\SDK\CurlHttpClient;
use Adquesto\SDK\PositioningSettings;
use Adquesto\SDK\ElementsContextProvider;
const API_URL = 'https://api.adquesto.com/v1/publishers/services/';
const SERVICE_UUID = '__paste Service UUID here__';
$options = [
'status' => 0,
'subscription' => 0,
'hasActiveCampaigns' => 0,
];
// dummy update_option and get_option implementation
// should persist option values
function update_option($name, $value) {
global $options;
$options[$name] = $value;
}
// should return array of Adquesto service options
function get_options() {
global $options;
return $options;
}
// basic webhook endpoint example
$response = ['status' => 'OK'];
$action = isset($_POST['action']) ? strtolower($_POST['action']) : null;
switch ($action) {
case 'questo_update_service_status_option':
case 'questo_update_subscription_option':
$service = new \Adquesto\SDK\Service(
new \Adquesto\SDK\ServiceAPIUrl(API_URL, SERVICE_UUID),
new CurlHttpClient
);
$serviceStatusResponse = $service->fetchStatus();
if (!$serviceStatusResponse) {
$response = [
'status' => 'ERROR',
'error' => 'Status fetch failed'
];
break;
}
foreach (get_options() as $optionName => $optionValue) {
update_option($optionName, (int)$serviceStatusResponse[$optionName]);
}
$response['options'] = get_options();
break;
case 'questo_force_update_javascript':
$adquesto = new Content(
API_URL,
SERVICE_UUID,
new InMemoryStorage,
new CurlHttpClient,
PositioningSettings::factory(PositioningSettings::STRATEGY_UPPER)
);
try {
$javascript = $adquesto->requestJavascript(true);
} catch (\Exception $e) {
$response = [
'status' => 'ERROR',
'error' => 'Javascript fetch failed: ' . $e->getMessage()
];
break;
}
$adquesto->getStorage()->set($javascript);
break;
default:
$response = [
'status' => 'ERROR',
'error' => 'Unsupported action'
];
}
header('Content-Type: application/json');
echo json_encode($response);