当点击类似facebook的按钮时,将用户重定向到另一个php页面

时间:2012-09-06 10:35:31

标签: php facebook api facebook-like

当用户点击“赞”按钮时,我无法将用户重定向到其他页面。 我在问这里之前一直在寻找。我找到了一些关于这方面的话题,但它仍然不适合我...

这是我的代码: 在此先感谢您的帮助!

<?php
require 'php-sdk/src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
            'appId' => '453976824647366',
            'secret' => 'XXXXXX',
        ));

// Get User ID
$user = $facebook->getUser();





if ($user) {
//    try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
//    } catch (FacebookApiException $e) {
//        error_log($e);
//        $user = null;
//    }
}

// Login or logout url will be needed depending on current user state.
if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();
} else {
    $loginUrl = $facebook->getLoginUrl();
}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <title>php-sdk</title>
        <style>
            body {
                font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
            }
            h1 a {
                text-decoration: none;
                color: #3b5998;
            }
            h1 a:hover {
                text-decoration: underline;
            }
        </style>
    </head>
    <body>

        <?php if ($user): ?>
            <a href="<?php echo $logoutUrl; ?>">Logout</a>
        <?php else: ?>
            <div>
                <a href="<?php echo $loginUrl; ?>">Login</a>
            </div>
        <?php endif ?>


        <?php if ($user): ?>
            <h3>Vous</h3>
            <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

            <h3>Personne connectée</h3>
    <!--            <pre><?php print_r($user_profile); ?></pre>-->

            <?php
            echo $user_profile['first_name'];

            echo '<a href="' . $user_profile['link'] . '">Go to my profile</a>';
            ?>

        <?php else: ?>
            <strong><em>Vous n'êtes pas connecté</em></strong>
        <?php endif ?>

        <div class="fb-like" data-href="http://apps.facebook.com/453976824647366/" data-width="450" data-show-faces="true"></div>

    </body>
    <div id="fb-root"></div>
    <script>(function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/fr_FR/all.js#xfbml=1&appId=453976824647366";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

    window.fbAsyncInit = function() {
        FB.init({appId: '453976824647366', status: true, cookie: true, xfbml: true});
        FB.Canvas.setSize({ width: 520, height: 1500 });
        FB.Event.subscribe('edge.create',
        function(response) {
            alert('like!');
            // put redirect code here eg
            window.location = "index2.php"; 
            location.href = 'index2.php';
        }
    );
    };


    </script>
</html>

2 个答案:

答案 0 :(得分:1)

单击“赞”按钮后重定向可以使用Facebook JavaScript事件edge.create处理:

FB.Event.subscribe('edge.create',
    function(response) {
        location.href = 'http://yourdomain.com/post/id';
    }
);

答案 1 :(得分:1)

要将网页重定向到其他网页,您可以使用fb javascript SDK,FB.Event.subscribe Event

FB.Event.subscribe('edge.create',function(response) {alert('You liked the URL: ' + response);});

有关详细信息,请访问http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/