In my facebook App, I am submitting the form to the same page.
After submitted, I will check the user is logged in by getUser(). If a user id can be got, POST data will be stored to database.
The below code works in Chrome, Firefox, Safari in Both Mac and PC.
However, when I first submit the form in IE 8, getUser() will return 0. Then I submit the form again, the user ID can be got.
I have not idea why the user ID cannot be got at the first submission. Can anyone help?
Thanks!
<?php
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET_KEY,
));
// Get User ID
$user = $facebook->getUser();
?>
<form action="" method="post" id="form1" name="form1">
...
</form>
I don't see the rest of code, but if you ask is you has $user, or not you have to request autentification or login. there is something like this:
if(!$user) make the login request
else continuos with your code.
sees the sdk php code or javascript library for Facebook in the Facebook developer side.
other problem is possible that you have and error while you try to get the user, and its possible you have to do a
try{
$user = $facebook->getUser();
}catch (exception *e){
//is there is and error you have to login again to get the users data
}
I hoper this will be useful for you
Related
During the past few hours I've been working on a "web app" for our hostesses. Its purpose is to login a facebook user and send him to next page where are like boxes with sign out button. This part is done, however I would like to make the process faster by inserting a likejacking script, so the user doesn't have to do anything but to log in and log out.
But I have one problem - how to check if the user is Fan or not (because if he already liked the page he will unlike it)? I've tried a solution which I'm using on my FB pages to fan gate, but it is not working outside facebook.
Here is the code I was trying to use:
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '0000000',
'secret' => '1654adadadada',
'cookie' => true
));
?>
<div id="fb-root"></div>
<?php
$signed_request = $facebook->getSignedRequest();
if ($signed_request['page']['liked'])
{
echo header('Location: liked.html');
}
else
{
echo header('Location: notliked.html');
}
your can get the data from FB graph.
https://graph.facebook.com/me/likes/PAGE_ID_TO_CHECK?access_token=XYZ
you will not be able to get this data without the token.
I've been working on a way to determine if a user likes a particular page so a tab on that page can be fangated or not. I didn't want to prompt the user for authorization for user_likes, so I avoided the JS SDK and used the PHP SDK:
<?php
require 'src/facebook.php';
$app_id = "...";
$app_secret = "...";
$facebook = new Facebook(array('appId' => $app_id, 'secret' => $app_secret, 'cookie' => true));
$signed_request = $facebook->getSignedRequest();
$like_status = $signed_request["page"]["liked"];
/* testing response */
if ($like_status) {
/* liked content */
} else {
/* not liked content */
}
?>
My problem is signed_request is passed only when the code is on the FB tab--if I hit the PHP page outside of FB, I get nothing. I wondered if there's a way to get this user info outside of Facebook.com.
You can try saving the signed_request in a session or cookie so you can use it until it expires. After which, the user will have to come through the tab again to renew it. Naturally, if you are planning to use cookies, you should keep the signed_request decoded so no one can find and use the access_token from the cookie.
Sounds like this isn't really feasible outside of the Facebook tab. We've begun deploying our tabs in PHP in order to pull this data without an action on the user's part.
Thanks!
Using the PHP SDK. I can log in and the app goes through the authentication ok (despite FB's greatest efforts to stop me!). The email displays courtesy of <?php print_r($user_profile); ?>
However, the array at the top of the example page always shows
[id] => 2
[oauth_uid] => 5
[oauth_provider] => facebook
[username] => 5
When I try to output the email, it again shows a value of 5. How do I grab the email address? Why is it displaying 5?
You need to do something like:
<?php
$user_profile = $facebook->api('/me');
print_r( $user_profile );
If you are asking for the email permission, the full email address will be visible.
I'm using Facebook OAuth interface but can't get immediate mode parameter working. Do you have any idea how that works with FB or any other url to use instead?!
(I don't/can't use FB JS libraries.)
A great way to make users login into your website with their Facebook account is to use the Facebook PHP SDK (see on github). So you will have something like :
require "facebook.php";
$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));
$user = $facebook->getUser();
If the user is logged in, then $user is his Facebook ID. You then have to check if you have a valid access token by making an API call :
If it does not raise any exception, then you have a valid access token
If it does, then you have to re-authenticate the user.
Here you go :
if ($user) {
try {
$facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
You need then to display the login or logout link :
<?php if ($user): ?>
Logout of Facebook
<?php else: ?>
Login with Facebook
<?php endif ?>
When the user is logged in and you have a valid access token, you can make API calls to get data from Facebook :
$user_profile = $facebook->api('/me');
You may want to check the example page of the Facebook PHP SDK which is well documented.
Hope that helps.
I have extracted following url from FB php-sdk (which Quentin reminds me):
https://www.facebook.com/extern/login_status.php
?api_key=<your app-id or api-key>
&no_user=<callback url>
&no_session=<callback url>
&ok_session=<callback url>
&session_version=3
When you redirects user to above url (or open it as an iframe or popup) Facebook silently/immediately redirects backs user/browser to:
no_user when user has NOT signed in.
no_session when user has signed in but has NOT authorized your app yet.
ok_session when user has signed in and already authorized your app. Additional parameters (user identification and required token) will be appended to this url by FB which you need to validate.
For example:
https://www.facebook.com/extern/login_status.php
?api_key=123456789012345
&no_user=http://example.com/signin/fb/no_user
&no_session=http://example.com/signin/fb/no_session
&ok_session=http://example.com/signin/fb/ok_session
&session_version=3
Put them in one line whit no spaces and don't forget to encode urls if needed.
See OpenID Immediate mode to find out why this is useful.
If you are developing java based application, in that case you can use SoicalAuth library.
http://code.google.com/p/socialauth/
I've the following code in my iframe Facebook app
<?php
/* include the PHP Facebook Client Library to help
with the API calls and make life easy */
require_once 'facebook.php';
$appapikey = 'key here';
$appsecret = 'secret here';
$facebook = new Facebook($appapikey, $appsecret);
$facebook->require_frame();
$user_id = $facebook->require_login();
?>
The problem is that the app keeps loading and reloading.
Is my implementation wrong?
You haven't added any check there. Everytime its just doing login. You have to check that user is logged or not if user is not logged then you have to run this code else do the action. I guess you must be having fb php sdk,check its demo example. If you dont got it download from here.