-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.php
92 lines (73 loc) · 2.37 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
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
<?php
include_once 'config.php'; // Defines APP_ID and APP_SECRET
include_once 'client/facebook.php';
class MyPageTab {
private $tabUrl;
public function __construct() {
// Initialise Facebook SDK
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
));
// Get the Facebook signed_request object
$signedRequest = $facebook->getSignedRequest();
// Get the app_data string parameter and decode it into an array
$appData = array();
if (!empty($signedRequest) && !empty($signedRequest['app_data'])) {
$appData = json_decode($signedRequest['app_data'], true);
}
// Get the colour parameter from app_data or use a default of blue
$colour = (!empty($appData['colour']) ? $appData['colour'] : 'blue');
// Echo the top of the HTML page
$this->echoPageTop($colour);
// Echo the page heading
if ($colour == 'red') {
echo '<h1>Red page</h1>';
} elseif ($colour == 'green') {
echo '<h1>Green page</h1>';
} elseif ($colour == 'blue') {
echo '<h1>Blue page</h1>';
}
// Echo a navigation menu
$this->tabUrl = 'http://www.facebook.com/apps/application.php?id=142464959162218&sk=app_142464959162218';
echo '<nav>';
$this->echoNavItem(array(
'colour' => 'red',
'another_param' => '5000'
));
$this->echoNavItem(array(
'colour' => 'green',
'id' => '12',
'list' => array('kangaroos & wallabies', 'koalas', 'wombats', 'emus')
));
$this->echoNavItem(array(
'colour' => 'blue'
));
echo '</nav>';
// Echo the app_data array
echo '<h2>app_data params</h2>';
echo '<pre>' . print_r($appData, 1) . '</pre>';
// Echo the bottom of the HTML page
$this->echoPageBottom();
}
private function echoNavItem($params) {
$encodedParams = urlencode(json_encode($params)); // Encode the parameters to a JSON string for use in a URL query string
$url = $this->tabUrl . '&app_data=' . $encodedParams;
echo '<a href="' . $url . '" target="_top">' . $params['colour'] . '</a> ';
}
private function echoPageTop($colour) {
echo "<!doctype html>\n" .
'<html>' .
'<head>' .
'<meta charset=utf-8>' .
'<title>Demo of app_data parameter</title>' .
'<style>body { font-family: sans-serif; color: #FFF; } a { color: #FFF; }</style>' .
'</head>' .
'<body style="background: '.$colour.'">';
}
private function echoPageBottom() {
echo '</body>' .
'</html>';
}
}
new MyPageTab();