我第一次使用 facebook API。
当用户单击“Page1.php”上的链接(例如网站的“Win Prize”)时,我正在这样做,所以它会出现“Page2.php”。
“Page2.php”将检查用户是否已登录 facebook 并喜欢 facebook 页面。
IF NOT 则显示 facebook like 按钮。当用户单击此按钮页面将被用户喜欢并重定向到“WinPrize.php”。
这是我的“Page2.php”代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<title>Facebook Page Like</title>
</head>
<body>
<?php
require 'src/facebook.php';
//Puted here APP ID
$app_id = "XXXXXXXXXXXX";
//Puted here app_secret key
$app_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXX";
//Putted here one pageID
$pageid="XXXXXXXX";
$appurl = "http://www.manektech.net/likeornot/";
//leave it as it is
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get User ID
$user = $facebook->getUser();
if($user)
{
try
{
// Proceed knowing you have a logged in user who's authenticated.
$likeID = $facebook->api(
array(
'method' => 'fql.query',
'query' => 'SELECT uid, page_id FROM page_fan WHERE uid="'.$user.'" AND page_id="'.$pageid.'"'
)
);
if(empty($likeID))
{ ?>
<div id="fb-root"></div>
<script type="text/javascript">
<!--
window.fbAsyncInit = function () {
//Putted here my orignal APP ID
FB.init({ appId: 'APP_ID', status: true, cookie: true, xfbml: true });
FB.Event.subscribe('edge.create', function (href, widget) {
window.location = "http://www.google.com";
});
};
(function () {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
} ());
//-->
</script>
<iframe src="XXX....XXX" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:35px;" allowTransparency="true"></iframe>
<?php }
else
{
header("Location: http://www.girlslovecars.com/spinwheel.php");
exit();
}
}
catch (FacebookApiException $e)
{
error_log($e);
}
}
if ($user)
{
$logoutUrl = $facebook->getLogoutUrl();
}
else
{
$params = array(
'scope' => 'user_likes,read_stream,publish_stream',
'redirect_uri' => $appurl
);
$loginUrl = $facebook->getLoginUrl($params);
}
?>
<?php if(!$user){ ?>
<div>
<a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
</div>
<?php } ?>
</body>
</html>
提前致谢