I just built an app with the Facebook PHP SDK and set it up. I am testing it and though I get the image to show up from my profile, the user info requested is not showing up. Here is the screenshot of the results
Here is the php code that I uploaded to the server. Note that the App ID and Secret have been removed from here.
<?php
require ('src/facebook.php');
$app_id = " "; //has been removed
$secret = " "; //has been removed
$app_url = "http://apps.facebook.com/instahomework/";
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk example 1 - user login and authorisation</title>
<style>
body { font-family: Helvetica, Verdana, Arial, sans-serif; }
a { text-decoration:none; }
a:hover { text-decoration:underline; }
</style>
</head>
<body>
<pre>
<?php print_r($_REQUEST); ?>
</pre>
<?php if ($user) { ?>
<h2>Welcome</h2>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture"><br/>
<?php echo $name; ?><br/>
<br/>
Gender : <?php echo $gender; ?><br/>
Locale : <?php echo $locale; ?><br/>
Username : <?php echo $username; ?><br/>
<?php } else { ?>
<strong><em>You have not granted this App permission to access your data. Redirecting to permissions request...</em></strong>
<?php } ?>
</body>
</html>
you should replace your HTML code with:
<h2>Welcome</h2>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture"><br/>
<?php echo $user_profile['name']; ?><br/>
<br/>
Gender : <?php echo $user_profile['gender']; ?><br/>
Locale : <?php echo $user_profile['locale']; ?><br/>
Username : <?php echo $user_profile['username']; ?><br/>
as the "user info" you want is requested by this line of code:
$user_profile = $facebook->api('/me');
and stored into the $user_profile array.
Another workaround (but not recommended) is using extract() to extract the data from facebook into your global variable scope, by adding this line of code:
extract($user_profile);
Related
This example is actually copied from Facebook.
When I access the page the behavior is strange:
The "login" first seems to work fine. Then it displays the 'Logout' link. But when I click it
I get the exact same screen again, with the 'Logout' link again. Refresh also gets to the same
screen.
Checking with Facebook, however, it DOES logs me out.
If I shut the browser and reopen it, the 'Login' is now correctly displayed.
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$params = array( 'next' => 'http://xxxxxxxxxx' );
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$params = array( 'redirect_uri' => 'http://xxxxxxxxxx' );
$loginUrl = $facebook->getLoginUrl($params);
}
?>
<!doctype html>
<html>
<head></head>
<body>
<?php if ($user): ?>
Logout
<?php else: ?>
Login with Facebook
<?php endif ?>
<?php if ($user): ?>
Picture = <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
User Object = <?php print_r($user_profile); ?>
<?php else: ?>
User is not Connected.
<?php endif ?>
</body>
</html>
$past = time() - 3600;
foreach ( $_COOKIE as $key => $value )
{
setcookie( $key, $value, $past, '/' );
}
you can try this code. It will just destroy all cookies saved from your site.
Its working for me..!
Create a file logout.php
<?php
session_start(); //start session
$_SESSION = array(); //clear session array
session_destroy(); //destroy session
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Log Out</title>
</head>
<body>
<p>You have successfully logged out!</p>
<p>Return to the Home page</p>
</body>
</html>
And change your code where you check user status
if ($user) {
$params = array( 'next' => 'http://....../logout.php' );
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$loginUrl = $facebook->getLoginUrl();
}
Use $logoutUrl for logout the user.
<?php if ($user): ?>
<?php echo "Welcome, ".$me['first_name']. " " .$me['last_name'] ." <br />";
echo "Id: " . $me['id'] ." <br />"; ?>
Logout <br />
<?php else: ?>
<a href="<?php echo $loginUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"> </a>
<?php endif ?>
Hope it will work fine
I'm trying to get user birthday with facebook API. I do know we need now to ask permission for this. I don't really know how it works
Just found this to add:
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(
array('req_perms' => 'email,read_stream')
);
}
<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>
and this is my whole code ( quite basic ):
<?php
require 'php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => '4539768XXXXX',
'secret' => 'fe86f3b0b34b3eXXXXXXXXXX',
'cookie' => true,
));
$signedRequest = $facebook->getSignedRequest();
$liked = $signedRequest["page"]["liked"];
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(
array('req_perms' => 'email,read_stream')
);
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<?php
if ($liked) {
header("Location:youlike.php");
} else {
echo "you don't like yet";
}
?>
<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login:
Login with Facebook
</div>
<?php endif ?>
<?php if ($user): ?>
<h3>Vous</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Personne connectée</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>Vous n'etes pas loggé!</em></strong>
<?php endif ?>
</body>
<script>
</script>
</html>
i would appreciate your help
thanks in advance!
You have a couple of problems in your code.
First, you aren't asking for the user_birthday permission when you create your $loginUrl. You should be requesting the same permissions for req_perms on line 33 as you do in the <fb:login-button> on line 68 of your code: email,user_birthday,status_update,publish_stream.
Once you copy over the permissions, you can remove line 68 of your code. The <fb:login-button> tag won't work without the JavaScript SDK.
Calling header() on line 61 after you've output text to the browser on lines 37-58 won't work. If you are going to redirect the browser, you can only do this before you've output text. Move this block of code before you output anything and it will work.
The white space between your ?> on line 36 and <!doctype> on line 39 will be output at the top of your HTML file. It cause the Facebook parser to choke. Some browsers don't like white space at the beginning of an HTML document either and will render your page incorrectly. Your code should really be ?><!doctype
I use the PHP SDK and follow the example. https://github.com/facebook/php-sdk
When I log in with Facebook and grant access, the page will redirect to my current page, but the url contains state and code. How to avoid displaying these parameters?
code is as follows:
"oauth.php":
<?php
require 'sdk/facebook.php';
$facebook = new Facebook(array(XXX,XXX));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl($params);
}
?>
+++++++++++++++++++
"index.php":
<?php include 'oauth.php'; ?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div>
<?php if ($user): ?>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<?php else: ?>
Login with Facebook
<?php endif ?>
</div>
</body>
</html>
try adding a header at the end of the code (after authentication)
header('Location:'.$currentPage);
I have been trying to get this to work for a few days on and off and seem to get no where with it. what ever I try I get
You are not Connected. Public profile of Naitik
I am using a shared ssl does this have any to do with this?
I am guessing its an OAuth 2.0 that is giving me problems?
if I go to https://www.facebook.com/dialog/oauth?client_id=IDNUMBER&redirect_uri=https://apps.facebook.com/... I can see all my info
but if I just click the app from facebook's homepage it will say I am not logged in?
any ideas?
Thank you!
<?php
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
Try adding a few more permissions like read_friendlist so that the API throws an exception on the statement $fb->api('/me') when the user is logged out as you want it to. For eg. set
$params = array('scope' => 'read_friendlists, friends_photos');
//$facebook->getLoginUrl($params);
//$facebook->getLogoutUrl($params);
// now use these login logout urls
You can also try adding $params = array('next'=> 'your-landing-page') to your facebook login request as $fb->loginUrl($params).
Im trying to connect to facebook using php-sdk. facebook login window appears when i click login, i fill my username and password and then it redirects to same page, saying not connected. I am new with facebook php. Help me
<?php
require_once("php-sdk/src/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '---',
'secret' => '---',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>
Please place your appId and secret and follow the code below. You should also use the latest Facebook PHP SDK
$facebook = new Facebook(array(
'appId' => '111111111',// your appId here
'secret' => '1a1a1a1a1a1a1',// your app secret here
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
You need to provide certain information to the app you just created. The call back url is needed. Please check the information at the facebook end.