点击Facebook就像按钮一样没有得到任何东西

时间:2012-08-22 14:09:40

标签: php facebook

我第一次使用facebook API。

我正在这样做,当用户点击“Page1.php”上的链接,例如网站的“Win Prize”时,它将变为“Page2.php”。

“Page2.php”将检查用户是否已在Facebook中登录并且喜欢了facebook页面。

如果没有,则显示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>

提前致谢

1 个答案:

答案 0 :(得分:1)

只有在使用XFBML嵌入like按钮时才会触发edge.create事件 - 它不适用于iframe版本(我想,你正在使用它,看到你的<iframe>元素脚本块之后的代码。)