forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetMetafyGuides.php
More file actions
122 lines (101 loc) · 3.95 KB
/
GetMetafyGuides.php
File metadata and controls
122 lines (101 loc) · 3.95 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
<?php
/**
* GetMetafyGuides.php
* Standalone endpoint to fetch Metafy community guides
* Accessible from anywhere (main menu, learn page, etc.)
*/
session_start();
session_write_close();
include "./Libraries/HTTPLibraries.php";
// CORS headers
SetHeaders();
$response = new stdClass();
// Get pagination parameters
$page = isset($_GET["page"]) ? intval($_GET["page"]) : 1;
$perPage = isset($_GET["per_page"]) ? intval($_GET["per_page"]) : 20;
// Ensure valid pagination values
$page = max(1, $page);
$perPage = min(max(1, $perPage), 100);
// Talishar community ID
$talisharCommunityId = "be5e01c0-02d1-4080-b601-c056d69b03f6";
// Metafy API key (can be set via environment variable or directly)
$metafyApiKey = getenv('METAFY_API_KEY') ?: (defined('METAFY_API_KEY') ? METAFY_API_KEY : 'api-x6S0fEVeEiIbmZGeHwL5fQA3sD0gD-dqZuMMKc-kXaI');
// Fetch guides from Metafy API using direct Bearer token authentication
if (!empty($metafyApiKey)) {
$allGuides = [];
$allMeta = null;
// First, fetch owner's guides from /v1/me/guides
$curlHandle = curl_init();
$ownerGuidesUrl = "https://metafy.gg/irk/api/v1/me/guides?page=" . $page . "&per_page=" . $perPage;
curl_setopt_array($curlHandle, [
CURLOPT_URL => $ownerGuidesUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer " . $metafyApiKey
]
]);
$ownerResponseBody = curl_exec($curlHandle);
$ownerHttpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle);
if ($ownerHttpCode === 200 && $ownerResponseBody) {
$ownerResponse = json_decode($ownerResponseBody);
if ($ownerResponse && isset($ownerResponse->guides)) {
// Mark owner's guides so we can identify them in the frontend
foreach ($ownerResponse->guides as $guide) {
$guide->isOwnerGuide = true;
}
$allGuides = array_merge($allGuides, $ownerResponse->guides);
$allMeta = $ownerResponse->meta;
}
}
// Then, fetch team/community guides from /v1/me/community/team/guides
$curlHandle = curl_init();
$teamGuidesUrl = "https://metafy.gg/irk/api/v1/me/community/team/guides?page=" . $page . "&per_page=" . $perPage;
curl_setopt_array($curlHandle, [
CURLOPT_URL => $teamGuidesUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer " . $metafyApiKey
]
]);
$teamResponseBody = curl_exec($curlHandle);
$teamHttpCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle);
if ($teamHttpCode === 200 && $teamResponseBody) {
$teamResponse = json_decode($teamResponseBody);
if ($teamResponse && isset($teamResponse->guides)) {
$allGuides = array_merge($allGuides, $teamResponse->guides);
}
}
// Build response with combined guides
$response->guides = $allGuides;
if ($allMeta) {
// Update pagination to reflect combined results
$response->meta = $allMeta;
$response->meta->pagination->total_guides = count($allGuides);
} else {
$response->meta = new stdClass();
$response->meta->pagination = new stdClass();
$response->meta->pagination->current_page = $page;
$response->meta->pagination->per_page = count($allGuides);
$response->meta->pagination->total_pages = 1;
$response->meta->pagination->total_guides = count($allGuides);
}
} else {
$response->error = "Metafy API key not configured";
$response->guides = [];
$response->meta = new stdClass();
$response->meta->pagination = new stdClass();
$response->meta->pagination->current_page = $page;
$response->meta->pagination->per_page = $perPage;
$response->meta->pagination->total_pages = 1;
}
// Debug: Add API key status to response
$response->_debug = new stdClass();
$response->_debug->has_api_key = !empty($metafyApiKey);
$response->_debug->api_key_prefix = substr($metafyApiKey, 0, 10) . "...";
echo json_encode($response);