-
Notifications
You must be signed in to change notification settings - Fork 1
/
outlook.php
140 lines (117 loc) · 5.28 KB
/
outlook.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
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
<?php
class OutlookService {
private static $outlookApiUrl = "https://outlook.office.com/api/v2.0";
public static function makeApiCall($access_token, $user_email, $method, $url, $payload = NULL) {
// Generate the list of headers to always send.
$headers = array(
"User-Agent: php-tutorial/1.0", // Sending a User-Agent header is a best practice.
"Authorization: Bearer " . $access_token, // Always need our auth token!
"Accept: application/json", // Always accept JSON response.
"client-request-id: " . self::makeGuid(), // Stamp each new request with a new GUID.
"return-client-request-id: true", // Tell the server to include our request-id GUID in the response.
"X-AnchorMailbox: " . $user_email // Provider user's email to optimize routing of API call
);
$curl = curl_init($url);
switch (strtoupper($method)) {
case "GET":
// Nothing to do, GET is the default and needs no
// extra headers.
error_log("Doing GET");
break;
case "POST":
error_log("Doing POST");
// Add a Content-Type header (IMPORTANT!)
$headers[] = "Content-Type: application/json";
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
break;
case "PATCH":
error_log("Doing PATCH");
// Add a Content-Type header (IMPORTANT!)
$headers[] = "Content-Type: application/json";
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
break;
case "DELETE":
error_log("Doing DELETE");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
error_log("INVALID METHOD: " . $method);
exit;
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
error_log("curl_exec done.");
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
error_log("Request returned status " . $httpCode);
if ($httpCode >= 400) {
return array('errorNumber' => $httpCode,
'error' => 'Request returned HTTP error ' . $httpCode);
}
$curl_errno = curl_errno($curl);
$curl_err = curl_error($curl);
if ($curl_errno) {
$msg = $curl_errno . ": " . $curl_err;
error_log("CURL returned an error: " . $msg);
curl_close($curl);
return array('errorNumber' => $curl_errno,
'error' => $msg);
} else {
error_log("Response: " . $response);
curl_close($curl);
return json_decode($response, true);
}
}
public static function getUser($access_token) {
$getUserParameters = array(
// Only return the user's display name and email address
"\$select" => "DisplayName,EmailAddress"
);
$getUserUrl = self::$outlookApiUrl . "/Me?" . http_build_query($getUserParameters);
return self::makeApiCall($access_token, "", "GET", $getUserUrl);
}
// This function generates a random GUID.
public static function makeGuid() {
if (function_exists('com_create_guid')) {
error_log("Using 'com_create_guid'.");
return strtolower(trim(com_create_guid(), '{}'));
} else {
error_log("Using custom GUID code.");
$charid = strtolower(md5(uniqid(rand(), true)));
$hyphen = chr(45);
$uuid = substr($charid, 0, 8) . $hyphen
. substr($charid, 8, 4) . $hyphen
. substr($charid, 12, 4) . $hyphen
. substr($charid, 16, 4) . $hyphen
. substr($charid, 20, 12);
return $uuid;
}
}
public static function getMessages($access_token, $user_email) {
$getMessagesParameters = array(
// Only return Subject, ReceivedDateTime, and From fields
"\$select" => "Subject,ReceivedDateTime,From",
// Sort by ReceivedDateTime, newest first
"\$orderby" => "ReceivedDateTime DESC",
// Return at most 10 results
"\$top" => "10"
);
$getMessagesUrl = self::$outlookApiUrl . "/Me/MailFolders/Inbox/Messages?" . http_build_query($getMessagesParameters);
return self::makeApiCall($access_token, $user_email, "GET", $getMessagesUrl);
}
public static function getEvents($access_token, $user_email) {
$getEventsParameters = array(
// Only return Subject, Start, and End fields
"\$select" => "Subject,Start,End",
// Sort by Start, oldest first
"\$orderby" => "Start/DateTime",
// Return at most 10 results
"\$top" => "10"
);
$getEventsUrl = self::$outlookApiUrl . "/Me/Events?" . http_build_query($getEventsParameters);
return self::makeApiCall($access_token, $user_email, "GET", $getEventsUrl);
}
}
?>