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 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测