-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.php
65 lines (62 loc) · 1.9 KB
/
post.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
<?php
// Handle the form submission on the server-side
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$access_token = $_POST["access_token"];
$page_id = $_POST["page_id"];
$message = $_POST["post_message"];
// Function to validate the access token
function validateAccessToken($access_token) {
$url = "https://graph.facebook.com/v12.0/me?access_token=" . $access_token;
$options = array(
"http" => array(
"header" => "Content-type: application/x-www-form-urlencoded",
"method" => "GET",
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
$data = json_decode($response, true);
if (!empty($data["id"])) {
return true;
} else {
return false;
}
}
// Function to post content on Facebook page
function post_on_facebook($access_token, $page_id, $message) {
$url = "https://graph.facebook.com/v12.0/{$page_id}/feed";
$params = array(
"access_token" => $access_token,
"message" => $message
);
$options = array(
"http" => array(
"header" => "Content-type: application/x-www-form-urlencoded",
"method" => "POST",
"content" => http_build_query($params)
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if (!empty($response)) {
return true;
} else {
return false;
}
}
// Perform form validation and posting on Facebook
if (empty($access_token) || empty($page_id) || empty($message)) {
echo "Please fill in all fields.";
} else {
if (validateAccessToken($access_token)) {
if (post_on_facebook($access_token, $page_id, $message)) {
echo "Post created successfully!!!";
} else {
echo "Failed to create a post.";
}
} else {
echo "Invalid access token. Please check and try again.";
}
}
}
?>