-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoniva-events-sample.php
115 lines (99 loc) · 2.63 KB
/
oniva-events-sample.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
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
<?php
// Set your Oniva domain and app token key
const ONIVA_URL = 'https://app-staging.oniva.dev/';
const APP_TOKEN_KEY = '8GySoDVTj8CytzmLngceh5zUSHVNNdQLjN3wg862';
const GRAPHQL_ENDPOINT = ONIVA_URL . 'api/graphql';
// GET THE AUTHENTICATION TOKEN
$authQuery = <<<'QUERY'
mutation Auth(
$key: String!
) {
authentication {
createTokenByAppToken(
appToken: { key: $key }
) {
token
}
}
}
QUERY;
$result = sendRequest($authQuery, ['key' => APP_TOKEN_KEY]);
if (!isset($result['data']['authentication']['createTokenByAppToken']['token'])) {
throw new Exception('Authentication error');
}
$token = $result['data']['authentication']['createTokenByAppToken']['token'];
// REQUEST THE EVENT TEASERS
$eventTeasersQuery = <<<'QUERY'
query EventTeasers(
$token: String!
$offset: Int!
$length: Int!
) {
viewer(token: $token) {
eventTeasers(limit: {offset: $offset, length: $length}) {
items {
id
title
lead
startDate
endDate
operations {
id
}
}
}
}
}
QUERY;
$result = sendRequest($eventTeasersQuery, ['token' => $token, 'offset' => 0, 'length' => 1]);
if (!isset($result['data']['viewer'])) {
throw new Exception('Invalid token');
}
printEventTeasers($result['data']['viewer']['eventTeasers']['items']);
/**
* Send the request and return the decoded result
*
* @param string $query
* @param array $variables
* @return array
*/
function sendRequest($query, array $variables = [])
{
$payload = [
'query' => $query,
'variables' => json_encode($variables),
];
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
];
$options = [
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $payload,
];
$curlHandle = curl_init(GRAPHQL_ENDPOINT);
curl_setopt_array($curlHandle, $options);
$result = curl_exec($curlHandle);
return json_decode($result, true);
}
/**
* Sample print of the eventTeasers
*
* @param array $eventTeasers
*/
function printEventTeasers(array $eventTeasers)
{
$outputEvent = <<<OUTPUT
<h1>%s</h1>
<small>%s - %s</small>
<p>%s</p>
<p>%s Sessions</p>
OUTPUT;
foreach ($eventTeasers as $event) {
$startDate = (new DateTime())->setTimestamp($event['startDate'])->format('d.m.Y H:i');
$endDate = (new DateTime())->setTimestamp($event['endDate'])->format('d.m.Y H:i');
printf($outputEvent, $event['title'], $startDate, $endDate, $event['lead'], count($event['operations']));
}
}