-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
90 lines (70 loc) · 2.57 KB
/
api.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
<?php
require_once 'defines.php';
require_once 'database.php';
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$data = json_decode(file_get_contents("php://input"));
$longLiveToken = getLongLiveToken($data);
$pageData = getPages($data, $longLiveToken);
echo json_encode($pageData);
} else {
http_response_code(405);
echo "Method not allowed.";
}
function getLongLiveToken($data)
{
global $conn;
$accessTokenEndpoint = ENDPOINT_BASE . 'oauth/access_token';
// endpoint params
$igParams = array(
'grant_type' => 'fb_exchange_token',
'client_id' => FACEBOOK_APP_ID,
'client_secret' => FACEBOOK_APP_SECRET,
'fb_exchange_token' => $data->accessToken
);
// add params to endpoint
$accessTokenEndpoint .= '?' . http_build_query($igParams);
// setup curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $accessTokenEndpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// make call and get response
$response = curl_exec($ch);
curl_close($ch);
$responseArray = json_decode($response, true);
$checkQuery = "select * from users where user_id = '" . $data->userId . "'";
if ($conn->query($checkQuery)->num_rows > 0) {
$sql = "update users set access_token = '" . $responseArray['access_token'] . "' where user_id = '" . $data->userId . "'";
} else {
$sql = "insert into users(user_id,access_token) values('" . $data->userId . "','" . $responseArray['access_token'] . "')";
}
if ($conn->query($sql)) {
return $responseArray['access_token'];
} else {
return false;
}
}
function getPages($data, $longLiveToken)
{
$pageAccessEndpoint = ENDPOINT_BASE . $data->userId . '/accounts';
// endpoint params
$igParams = array(
'access_token' => $longLiveToken
);
// add params to endpoint
$pageAccessEndpoint .= '?' . http_build_query($igParams);
// setup curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $pageAccessEndpoint);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// make call and get response
$response = curl_exec($ch);
curl_close($ch);
$responseArray = json_decode($response, true);
$responseArray['userId'] = $data->userId;
return $responseArray;
}
$conn->close();