繁体   English   中英

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

[英]Redirect user to another php page when a facebook like button is clicked

我点击类似按钮时无法将用户重定向到另一个页面。 我在问这里之前一直在寻找。 我发现了一些关于这个的话题,但它仍然不适合我......

这是我的代码:提前感谢您的帮助!

<?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>

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

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

要将页面重定向到另一个页面,您可以使用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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM