Facebook iFrame app - how to fetch Preload FQL result using PHP SDK?

孤街浪徒 提交于 2019-12-21 05:15:20

问题


since few years I have an FBML app (a small Flash game) which I'm now trying to convert to an iFrame app. Unfortunately there aren't many docs for Facebook iFrame apps yet.

For my game I need the user's first name, picture, gender and the city.

In my old version I had this preload FQL (created once by a PHP script):

$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)));

and then my FBML app script had been as simple as:

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

?>

Does anybody please have hints on how to recreate the same script for the iFrame version - i.e. how can I fetch the result of Preload FQL by my iFrame app?

According to an older Facebook blog entry Preload FQL should be accessible by the iFrame apps.

Thank you! Alex


回答1:


My own answer after long searching is that Preload FQL results aren't sent to iframe Facebook apps.

That is why Facebook performance doc says:

"Preload FQL Query and Multiquery.

This section applies to FBML canvas pages, but not to websites or IFrame canvas pages."




回答2:


As Facebook said for Preload FQL

"Facebook will send the result of these FQL queries as JSON-encoded POST parameters to your Canvas URL"

print_r your $_POST and see which variable is the "json-encoded results". You convert json into php object using json_decode

JSON looks like this: {"var":"val","var":"val"}

Also, Facebook already has great docs for iframes. Then you might have missed these great docs:

Facebook Docs Home

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

Authentication

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

Signed Request

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

iFrame Canvas Apps

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

PHP SDK

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

Graph API

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




回答3:


You don't need to call any FQL for the information you are getting. For iFrame you just need to do following steps

  1. Download the PHP SDK of graph api https://github.com/facebook/php-sdk/

  2. Create the object and authorize the app from user

     $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>';
    }
    

    // You can found all the data in this array. print_r($fbme);

For more detail you can follow this tutorial http://thinkdiff.net/facebook/php-sdk-graph-api-base-facebook-connect-tutorial/

Hope it works for you



来源:https://stackoverflow.com/questions/5894992/facebook-iframe-app-how-to-fetch-preload-fql-result-using-php-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!