-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
61 lines (47 loc) · 2.03 KB
/
index.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
<?php
// config
define('APP_ID', '');
define('APP_SECRET', '');
define("PAGE_ID", "");
define("SCRIPT_URL", "http://example.com"); // URL for redirect back from FB
function doWallPost($postName = '', $postMessage = '', $postLink = '', $postCaption = '', $postDescription = '') {
$FB_APP_ID = APP_ID;
$FB_APP_SECRET = APP_SECRET;
$code = $_REQUEST["code"];
if (empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" . $FB_APP_ID . "&redirect_uri=" . urlencode(SCRIPT_URL) . "&scope=publish_stream";
header("Location: $dialog_url");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=" . $FB_APP_ID . "&redirect_uri=" . urlencode(SCRIPT_URL) . "&client_secret=" . $FB_APP_SECRET . "&code=" . $code;
$access_token = file_get_contents($token_url);
$param1 = explode("&", $access_token);
$param2 = explode("=", $param1[0]);
$FB_ACCESS_TOKEN = $param2[1];
$token_url = "https://graph.facebook.com/" . PAGE_ID . "/?fields=access_token&access_token=" . $FB_ACCESS_TOKEN;
$pageAccessToken = file_get_contents($token_url);
$pageAccessToken = json_decode($pageAccessToken, true);
$FB_ACCESS_TOKEN = $pageAccessToken["access_token"];
$url = "https://graph.facebook.com/" . PAGE_ID . "/feed";
$attachment = array(
'access_token' => $FB_ACCESS_TOKEN,
'name' => $postName,
'link' => $postLink,
'description' => $postDescription,
'message' => $postMessage,
'caption' => $postCaption
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
$result = curl_exec($ch);
header('Content-type:text/html');
curl_close($ch);
return $result;
}
// Test post
doWallPost("Test", "test");