检测像Facebook的JavaScript API + iFrame [英] Detect Like with Facebook JavaScript API + iFrame

查看:194
本文介绍了检测像Facebook的JavaScript API + iFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想检测他们是否喜欢当前页面。通常我会在PHP中使用print_r($ _ REQUEST),但在使用iframe时似乎不起作用。



还有这个选项: http://developers.facebook.com/docs/reference/fbml/visible-to-connection / ,但它表示它已经弃用,我从来没有喜欢这个方法,因为它是相当黑客。



现在该怎么办?喜欢使用XFBML + JavaScript API,但如果需要,可以使用PHP。

解决方案

我们已经完成了这几次,似乎工作得很好它使用XFBML生成 Like Button widget 和JS SDK来呈现XFBML并订阅 Facebook活动。代码示例如下:



编辑:由于您正在寻找在页面加载时检测用户是否为风扇,并且FB已弃用功能,让您通过将 fb_page_id 传递到地址查询字符串加载画布时直接从中获取它,您需要一个应用程序安装才能测试用户的风扇 - 你的页面这对你的应用程序来说肯定会增加很多的摩擦力,但是它现在是现在的 - 我猜。

  php 
require'facebook.php';

//创建我们的应用程序实例(将其替换为您的appId和密码)。
$ facebook = new Facebook(array(
'appId'=>'你的APP ID',
'secret'=>'你的APP SECRET',
'cookie '=> false,
));

try
{
$ session = $ facebook-> getSession();

if(empty($ session ['uid']))
{
抛出新异常(用户未连接到应用程序);
}

$ is_fan = $ facebook-> api(array(
'method'=>'fql.query',
'query'=> ;select uid,page_id FROM page_fan WHERE uid = {$ session ['uid']}
));

if(false == $ is_fan || count($ is_fan)== 0)// 0如果用户不是风扇,返回结果
{
$ is_fan = false;
}
else
{
$ is_fan = true;
}
}
catch(FacebookApiException $ e)
{
/ **
*您没有活动的用户会话或必需的权限
*为这个用户,所以rdr到Facebook登录。
** /

$ loginUrl = $ facebook-> getLoginUrl(array(
'req_perms'=>'user_likes'
));

header('Location:'。$ loginUrl);
退出;
}
?>
< html>
< head>
< / head>
< body>

<? if(empty($ is_fan))://用户不是风扇。 ?>
< fb:like href =http://www.facebook.com/your-facebook-page
show_faces =true
width =400>
< / fb:like>
<其他:?>
Yay!你是粉丝!
<万一; > ;?

< div id =fb-root>< / div>
< script src =http://connect.facebook.net/en_US/all.jstype =text / javascript>
< / script>
< script type =text / javascript>
FB.init({
appId:'<?= FB_APP_ID;?>',
cookie:true,
status:true,
xfbml:true
});

//订阅边缘创建事件,从Facebook
FB.Event.subscribe('edge.create',function(response)
{
alert 恭喜!我们很兴奋,现在你是粉丝!)
});

< / script>

< / body>
< / html>

好的,终于有了所有格式化的直线降价。这不是痛苦的。(sike):|


Building an app with the Facebook JavaScript API that will embedded into a page using the new iframe method.

I want to detect if they have liked the current page. Usually I would use print_r($_REQUEST) in PHP but that doesn't seem to work when using an iframe.

There is also this option: http://developers.facebook.com/docs/reference/fbml/visible-to-connection/ but it says its deprecated and I have never liked this method as its fairly hacky.

What is the way t do it now? Prefer to use XFBML + JavaScript API but can use PHP if required.

解决方案

We've done this several times, and it seems to work pretty well. It uses XFBML to generate a Like Button widget and the JS SDK to render XFBML and subscribe to Facebook events. Code sample below:

edit: Since you're looking to detect if the user is a fan when the page loads, and FB deprecated the feature to let you get it directly from them when the canvas is loaded by passing fb_page_id to the address query string, you'll need an application install for the user to test their fan-dom of your page. It certainly adds a lot of friction to your application, but it is what it is for now - I guess.

    <?php
require 'facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
    'appId'  => 'YOUR APP ID',
    'secret' => 'YOUR APP SECRET',
    'cookie' => false,
));

try
{
    $session = $facebook->getSession();

    if (empty($session['uid']))
    {
        throw new Exception("User not connected to application.");
    }

    $is_fan = $facebook->api(array(
        'method'    => 'fql.query',
        'query'     => "SELECT uid, page_id FROM page_fan WHERE uid = {$session['uid']}"
    ));

    if (false == $is_fan || count($is_fan) == 0) // 0 results will be returned if the user is not a fan
    {
        $is_fan = false;
    }
    else
    {
        $is_fan = true;
    }   
}
catch (FacebookApiException $e)
{
    /**
     * you don't have an active user session or required permissions 
     * for this user, so rdr to facebook to login.
    **/

    $loginUrl = $facebook->getLoginUrl(array(
        'req_perms' => 'user_likes'
    ));

    header('Location: ' . $loginUrl);
    exit;
}
?>
<html>
<head>
</head>
<body>

<? if (empty($is_fan)): //user is not a fan. ?>
    <fb:like href="http://www.facebook.com/your-facebook-page"
        show_faces="true"
        width="400">
    </fb:like>
<? else: ?>
    Yay! You're a fan!
<? endif; >?

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript">
    </script>
<script type="text/javascript">
FB.init({
    appId: '<?= FB_APP_ID; ?>',
    cookie: true,
    status: true,
    xfbml: true
});

// Subscribe to the edge creation event, from Facebook
FB.Event.subscribe('edge.create', function(response)
{
    alert("Congratulations! We are so excited that you are a fan now! woot!")
});

</script>

</body>
</html>

okay, finally got got everything formatted with straight markdown. that wasn't painful at all.. (sike) :|

这篇关于检测像Facebook的JavaScript API + iFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆