-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_aliases.php
More file actions
96 lines (77 loc) · 3.37 KB
/
get_aliases.php
File metadata and controls
96 lines (77 loc) · 3.37 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
<?php
header('Content-Type: application/json'); // Tell the browser this is JSON
// IMPORTANT: Replace with your actual POSTALEAPIKEY
$baseApiUrl = 'https://postale.io/api/v1/aliases';
$username = 'POSTALEAPIKEY';
// Initialize variables for pagination
$allMailboxes = [];
$currentPage = 0;
$pageSize = 25; // Default page size, or you might get this from the API's 'paging' field in the first response.
// Assuming the API returns 'paging' as the number of items per page.
$totalMailboxes = 0;
$lastPage = 0;
$morePages = true;
do {
// Construct the URL with current page and page size
// Adjust parameter names (e.g., 'page', 'pageSize') based on your API's documentation
$apiUrl = $baseApiUrl . "?page=" . $currentPage . "&pageSize=" . $pageSize; // Example query parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
http_response_code(500);
echo json_encode(['error' => 'cURL error: ' . $error_msg, 'fetchedPages' => count($allMailboxes)]);
curl_close($ch);
exit; // Stop execution on cURL error
}
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code >= 400) {
http_response_code($http_code);
echo json_encode(['error' => 'API returned an error', 'status_code' => $http_code, 'details' => $response, 'fetchedPages' => count($allMailboxes)]);
curl_close($ch);
exit; // Stop execution on API error
}
$data = json_decode($response, true); // Decode response into an associative array
// Check if the decoded data is valid and contains 'results'
if (json_last_error() !== JSON_ERROR_NONE || !isset($data['results'])) {
http_response_code(500);
echo json_encode(['error' => 'Invalid JSON response or missing results array from API', 'rawResponse' => $response]);
curl_close($ch);
exit;
}
// Append current page's results
$allMailboxes = array_merge($allMailboxes, $data['results']);
// Update pagination variables for the next loop iteration
if (isset($data['page'])) {
$currentPage = $data['page'];
}
if (isset($data['paging'])) {
$pageSize = $data['paging']; // Use the API's preferred page size for subsequent requests
}
if (isset($data['totalCount'])) {
$totalMailboxes = $data['totalCount'];
}
if (isset($data['lastPage'])) {
$lastPage = $data['lastPage'];
}
curl_close($ch); // Close cURL for this iteration
// Determine if there are more pages
// Method 1: Use 'lastPage' field if available (most robust)
if (isset($data['lastPage'])) {
$morePages = ($currentPage < $lastPage);
}
// Method 2: Fallback if 'lastPage' is not reliable/present, check if we received a full page
else {
$morePages = (count($data['results']) == $pageSize);
}
$currentPage++; // Increment page for the next request
// Small delay to avoid hammering the API, especially in a tight loop
// usleep(50000); // 50ms delay, uncomment if needed
} while ($morePages);
// Output the consolidated list of all mailboxes
echo json_encode(['results' => $allMailboxes]);
?>