doushe7934 2014-11-20 16:57
浏览 28
已采纳

Facebook PHP SDK无法通过AJAX工作

The situation: I have a finished Facebook canvas app (PHP/CodeIgniter), I just need to add the Facebook-related options such as sharing and inviting friends.

Current task: getting the list of invitable friends from the PHP SDK.

Relevant documentation: https://developers.facebook.com/docs/games/invitable-friends/v2.0

The code: in my Facebook library, I have created the following function.

/**
    * Returns the current user's invitable friends
    */
    public function get_invitable_friends() {
        if ( $this->session ) {
            $request = ( new FacebookRequest( $this->session, 'GET', '/me/invitable_friends' ) )->execute();

            $graphObject = $request->getGraphObject();

            return $graphObject;
        }
        return false;
    }

The condition if($this->session) is because it doesn't make sense to try anything if there's no Facebook session in the first place. This will come into play later.

I've tried calling this function in two ways. The first way works and the second doesn't. I'm gonna present both, and then somebody will hopefully explain to me why the second way doesn't work and how to fix it, as I'd much prefer to use that.

First (working) way:

Call the function from the main controller's index() method, as the page loads. The function correctly returns a list of my friends.

Second (non-working) way:

Create this function in the controller:

//load list of friends we can invite to play the game
    public function load_invitable_friends()
    {
        $this->load->library('facebook');

        $list = $this->facebook->get_invitable_friends();

        var_dump($list);
    }

Then call it through AJAX, like:

function loadInvites(){
    $.post(base+"main/load_invitable_friends/",function(resp){
        $('#slide_6_inner').html(resp);
    });
}

After this call, the content of the slide_6_inner div should contain the list of friends, as dumped by var_dump. However, the content is bool(false), indicating that the Facebook session is no longer present.

If I remove the condition if( $this->session ) from the get_invitable_friends() method, then this error happens:

A PHP Error was encountered

Severity: 4096

Message: Argument 1 passed to Facebook\FacebookRequest::__construct() must be an instance of Facebook\FacebookSession, null given, called in /home/lights/public_html/appname/application/libraries/facebook/Facebook.php on line 125 and defined

Filename: Facebook/FacebookRequest.php

Line Number: 182

The session in Facebook.php is initially created with the following code.

$this->ci =& get_instance();
        // Initialize the SDK
        FacebookSession::setDefaultApplication( $api_id, $api_secret) );

        $this->helper = new FacebookCanvasLoginHelper();
        $this->session = $this->helper->getSession();

To sum up - why is this problem occurring and how do I fix it?

  • 写回答

2条回答 默认 最新

  • doupaimo8288 2014-11-24 17:20
    关注

    In order to retrieve the session from the server side, you will need to first create a session using the FacebookCanvasLoginHelper. This class takes a signed request from Facebook supplied in a POST request, and exchanges it for a FacebookSession object. This should only really be done one when a user logs into your application:

    $helper = new FacebookCanvasLoginHelper();
    try {
      $session = $helper->getSession();
    } catch (FacebookRequestException $ex) {
        // When Facebook returns an error
    } catch (\Exception $ex) {
        // When validation fails or other local issues  
    }
    if ($session) {
      // Store this in your PHP session somewhere:
      $token = $session->getToken();
    }
    

    Thereafter, you should use the token to initialize the session:

    $token = //GET THIS FROM SESSION
    use Facebook\FacebookSession;
    
    FacebookSession::setDefaultApplication('app-id', 'app-secret');
    
    // If you already have a valid access token:
    $session = new FacebookSession($token);
    
    // To validate the session:
    try {
      $session->validate();
    } catch (FacebookRequestException $ex) {
      // Session not valid, Graph API returned an exception with the reason.
      echo $ex->getMessage();
    } catch (\Exception $ex) {
      // Graph API returned info, but it may mismatch the current app or have expired.
      echo $ex->getMessage();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题