Facebook iFrame应用程序 - 如何使用PHP SDK获取Preload FQL结果?

时间:2011-05-05 08:51:34

标签: facebook iframe facebook-fql facebook-iframe

因为几年我有一个FBML应用程序(一个小Flash game),我现在正试图转换为iFrame应用程序。不幸的是,Facebook iFrame应用程序的文档还不多。

对于我的游戏,我需要用户的名字,图片,性别和城市。

在我的旧版本中,我有这个预加载FQL(由PHP脚本创建一次):

$fql = array('info' => array('pattern' => 'facebook',
    'query' => 'SELECT first_name, sex, pic_big, current_location 
    FROM user WHERE uid={*user*}'));

$fb->api_client->admin_setAppProperties(
    array('preload_fql' => json_encode($fql)));

然后我的FBML应用程序脚本就像:

一样简单
<?php

require_once('facebook.php');

define('FB_API_ID', 'XXX');
define('FB_AUTH_SECRET', 'YYY');

$fb         = new Facebook(FB_API_ID, FB_AUTH_SECRET);
$viewer_id  = $fb->require_login();
$data       = json_decode($fb->fb_params['info'], true);

$first_name = $data[0][0];
$last_name  = $data[0][2];
$female     = ($data[0][3] != 'male');
$avatar     = $data[0][3];
$city       = $data[0][4]['city'];

# and then I'd just construct flashvars attribute
# for the <fb:swf ...> tag and print it

?>

有人请提示如何为iFrame版本重新创建相同的脚本 - 即如何通过我的iFrame应用获取Preload FQL的结果?

根据较早的Facebook博客文章Preload FQL should be accessible by the iFrame apps

谢谢! 亚历

3 个答案:

答案 0 :(得分:3)

经过长时间搜索,我自己的答案是Preload FQL结果不会发送到iframe Facebook应用程序。

这就是为什么Facebook performance doc说:

“预加载FQL查询和多重查询。

本节适用于FBML画布页面,但不适用于网站或IFrame画布页面。“

答案 1 :(得分:0)

正如Facebook所说的Preload FQL

Facebook会将这些FQL查询的结果作为JSON编码的POST参数发送到您的Canvas URL

print_r 您的 $ _ POST ,看看哪个变量是“ json-encoded results ”。使用json_decode

将json转换为php对象

JSON看起来像这样:{"var":"val","var":"val"}

此外,Facebook已经有很好的iframe文档。然后你可能错过了这些伟大的文档:

Facebook Docs Home

http://developers.facebook.com/docs/

验证

http://developers.facebook.com/docs/authentication/

签名请求

http://developers.facebook.com/docs/authentication/signed_request/

iFrame Canvas应用

http://developers.facebook.com/docs/guides/canvas/

PHP SDK

https://github.com/facebook/php-sdk

图谱API

http://developers.facebook.com/docs/reference/api/

答案 2 :(得分:0)

您无需为获得的信息调用任何FQL。对于iFrame,您只需执行以下步骤

  1. 下载图表api https://github.com/facebook/php-sdk/

  2. 的PHP SDK
  3. 创建对象并从用户

    授权应用程序
     $fbconfig['appid' ]  = "your application id";
    
    $fbconfig['api'   ]  = "your application api key";
    
    $fbconfig['secret']  = "your application secret key";
    
    
    
    
    try{
        include_once "facebook.php";
    }
    catch(Exception $o){
        echo '<pre>';
        print_r($o);
        echo '</pre>';
    }
    // Create our Application instance.
    $facebook = new Facebook(array(
      'appId'  => $fbconfig['appid'],
      'secret' => $fbconfig['secret'],
      'cookie' => true,
    ));
    // User location extended permission allow you to get user's current location
    $loginparams = array('canvas' => 1,'fbconnect' => 0,'req_perms' => 'user_location');
    
    $loginUrl = $facebook->getLoginUrl($loginparams);
    
    
    // We may or may not have this data based on a $_GET or $_COOKIE based session.
    // If we get a session here, it means we found a correctly signed session using
    // the Application Secret only Facebook and the Application know. We dont know
    // if it is still valid until we make an API call using the session. A session
    // can become invalid if it has already expired (should not be getting the
    // session back in this case) or if the user logged out of Facebook.
    $session = $facebook->getSession();
    
    $fbme = null;
    // Session based graph API call.
    if ($session) {
      try {
        $uid = $facebook->getUser();
        $fbme = $facebook->api('/me');
      } catch (FacebookApiException $e) {
          d($e);
      }
    }
    
    function d($d){
        echo '<pre>';
        print_r($d);
        echo '</pre>';
    }
    

    //您可以找到此数组中的所有数据。 的print_r($ fbme);

  4. 有关详细信息,请参阅本教程http://thinkdiff.net/facebook/php-sdk-graph-api-base-facebook-connect-tutorial/

    希望它适合你