<?php
require 'facebook.php';
$base_url = "http://promos.uk.glam.com/splash_test/example.php";
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxx',
'secret' => 'yyyyyyyyyyyyyy',
'cookie' => true,
));
function debug($e, $name = '') {
echo '<p><pre>'.$name.' : '; print_r($e); echo '</pre></p>';
}
// We may or may not have this data based on a $_GET or $_COOKIE based session.
//
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
debug($facebook->api('/me'), 'Me');
debug($facebook, 'FB');
//We seem to have a valid session, lets post the evetn
} catch (FacebookApiException $e) {
debug($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl(array('next' => $base_url));
} else {
$loginUrl = $facebook->getLoginUrl(
array(
'display' => 'popup',
'next' => $base_url . '?loginsucc=1',
'cancel_url'=> $base_url . '?cancel=1',
'req_perms' => 'create_event')
);
}
// if user click cancel in the popup window
if (isset($_REQUEST['cancel'])) {
die("<script>
window.close();
</script>");
} elseif($me && isset($_REQUEST['loginsucc'])) {
//only if valid session found and loginsucc is set
//after facebook redirects it will send a session parameter as a json value
//now decode them, make them array and sort based on keys
$sortArray = get_object_vars(json_decode($_GET['session']));
ksort($sortArray);
$strCookie = "";
$flag = false;
foreach($sortArray as $key=>$item){
if ($flag) $strCookie .= '&';
$strCookie .= $key . '=' . $item;
$flag = true;
}
//now set the cookie so that next time user don't need to click login again
setCookie('fbs_xxxxxxxxxxxxx', $strCookie);
die("<script>
window.close();
window.opener.location.reload();
</script>");
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Create an event on Facebook</title>
</head>
<body>
<?php
//if user is logged in and session is valid.
if ($uid){
try {
$file = "end300.jpg";
$event_info = '{"privacy_type":"SECRET", "name":"Rest Test PHP 2","host":"Me","start_time":1277560966,"end_time":1277571766,"location":"Cinema, London","description":"A testing event \u000d\u000a created by the REST API"}';
$call = array('method' => 'events.create', 'event_info' => $event_info);//, '@'.realpath($file).';type=image/jpg');
debug($facebook->api($call), 'call');
} catch (FacebookApiException $e) {
debug($e);
}
echo '<a href="' . $facebook->getLogoutUrl() . '">Logout</a>';
} else {
?>
<script type="text/javascript">
var newwindow;
var intId;
function login(){
var screenX = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft,
screenY = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop,
outerWidth = typeof window.outerWidth != 'undefined' ? window.outerWidth : document.body.clientWidth,
outerHeight = typeof window.outerHeight != 'undefined' ? window.outerHeight : (document.body.clientHeight - 22),
width = 500,
height = 270,
left = parseInt(screenX + ((outerWidth - width) / 2), 10),
top = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
features = (
'width=' + width +
',height=' + height +
',left=' + left +
',top=' + top
);
newwindow=window.open('<?=$loginUrl?>','Login by facebook',features);
if (window.focus) {newwindow.focus()}
return false;
}
</script>
Please login to Facebook and we will setup the event for you!
<a href="#" onclick="login();return false;">
<img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif" border="0">
</a>
<?php
}
?>
</body>
</html>