I have a related forum post on this topic, but I think my wording of the problem was misleading, so I’ve created another topic.
Also, I’ve read everything in this forum on this subject, and haven’t found what I need, so kindly read this carefully before replying; I’d rather not be sent to another thread which is not what I’m looking for.
Here’s what I am looking for: a simple, working, complete example of a Yii application which:
Uses Facebook’s Javascript SDK
Uses the Graph API PHP module provided by Facebook
I do not want to re-do the way Facebook performs authentication, I.e., I do not want to build a Yii login as recommended in this forum thread:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.Event.subscribe('auth.sessionChange', function(response) {
if (response.session) {
// A user has logged in, and a new cookie has been saved
jQuery.post(
'<?php echo Yii::app()->controller->createUrl('xxxxx') ?>',
jQuery.extend({service: 'facebook'}, response.session)
);
}
});
FB.init({appId: '<?php echo $appid?>', status: true, cookie: true, xfbml: true});
</script>
This snippet will detect if the user have an active session with facebook. It will post the user session to the xxxx url if there is a active session. From memory for this to work, the user must already sign up with your application. If you have not done that yet, you need to setup oauth and redirect the user to facebook to grant their permission to your app.
$appid is the facebook id that you get when you register your app with Facebook.
You would want to create a Widget and put those in the widget.
Thanks so much for your post. It looks as though what you’ve written is listening for a session changing event. Again, what I"m really interested in is a working example which gets the login status of the user. 99% of the time, my user is ALREADY logged into facebook; I just need to know his or her Facebook user id.
And, most of all, I’d like to see a complete example. I’ve gotten a Facebook app to work outside of the Yii framework. But somethng about the way the Yii framework operates is making it impossible for me to use Facebook’s session.
A complete example would be one which specifies, e.g., “put this javascript into index.php”… “add this action to the main controller”… etc. Can anyone provide me a small, working–but COMPLETE Facebook app, which does what I’ve described? Again, I’d be willing to pay for this; I’ve struggled too long to figure this out and I am not a Javascript expert.
The facebook PHP SDK, distributed by facebook as “Facebook.php”, attempts to set the local session using PHP’s built-in session handling. But Yii handles sessions differently, using CHttpCookie.
So, the fix involved these steps:
Modify the Facebook.php file distributed by Facebook.
Wherever the built-in PHP cookies are referred to, use
Yii’s cookies instead. There are several spots to edit.
$yiiCookie = new CHttpCookie($cookieName, $value);
$yiiCookie->expire = time() + 31536000; // one year from now
Yii::app()->request->cookies[$cookieName] = $yiiCookie;
In the main layout file protected/views/layout/main.php, we can now get/set the session variable
like this:
// Get an instance of the Facebook class distributed as the PHP SDK by facebook:
$facebook = new Facebook(array(
'appId' => 'MY-APP-ID',
'secret' => 'MY-APP-SECRET',
'cookie' => true,
));
$session = $facebook->getSession();
$encodedSession = json_encode($session);
if ($session) {
$facebook->setSession($session, true);
// THE KEY TO THE PUZZLE IS THE LINE BELOW, which sets your local session variable to Facebook's session:
$facebook->setCookieFromSession($session);
// And now do whate'er you fancy with facebook's API, e.g., grabbing the user's profile:
$facebookUser = $facebook->api('/me');
// Or perhaps a FQL query:
$query = 'SELECT app_id, canvas_name FROM application WHERE canvas_name="onthefarm"';
$apps = $facebook->api_client->fql_query($query);
}
Thanks for the great suggestions and I hope I’ve spared someone a future migraine.
I want to note that I moved that main.php code into my base controller class which has freed me up to get the user’s FB id (or other information about the FB session) anywhere I want:
Add this to the top of your ‘protected/components/Controller.php’ file:
Adding the beforeSave and beforeFind code to the models allows you to have very simple row level security (only showing users their own information) pretty easily.
you haven’t create an object instance to access facebook php sdk in your above mention code that is why this error is generating…please have a look again in your code…!!
I am using facebook PHP SDK 3.1.1 and Yii 1.1.8, your solution not works with that version. It not contains setcookie() function in facebook.php file. I am able to store the value into session through Yii::app()->session but when reading the session, it returns empty value instead of actual value. I don’t have any idea how to resolve this issue. Please give me an idea.