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?