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
Related
I am trying to make a php facebook app. Downloaded the Facebook php sdk and used the example.php (with my own appId and secret) to try to get a example app running. I can use the example app, but when I click login with facebook and and authenticate the app it throws this exception:
"Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in /var/www/facebook_test/base_facebook.php on line 1264"
Below is the code I am using.
Any guidance would be greatly appreciated.
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'myappid',
'secret' => 'mysecretid',
'cookie' => true,
));
// 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 {
$statusUrl = $facebook->getLoginStatusUrl();
$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>
Check the login status using OAuth 2.0 handled by the PHP SDK:
Check the login status
</div>
<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>
Edit:
This is the stack trace:
#0 /var/www/facebook_test/base_facebook.php(873):
BaseFacebook->throwAPIException(Array) #1 [internal function]:
BaseFacebook->_graph('/naitik') #2 /var/www/facebook_test/base_facebook.php(647):
call_user_func_array(Array, Array) #3 /var/www/facebook_test/index.php(66):
BaseFacebook->api('/naitik') #4 {main}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
Apparently it doesn't!
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);
I needed to request permission to a facebook user to get specific informations (email,birthday).
When a user launches the application for the first time, if he accepts the permission, he is forced to refresh the page to display data (just informations concerning the user,see my code).
The problem is it works fine sometime,that's why i don't know if it is a facebook problem or a problem in my own code.
If you can have a look at my code, some help would be much appreciated!
thanks in advance
<?php
require 'php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => '453976824647366',
'secret' => 'fe86f3b0b34b3ed6XXXXXX',
'cookie' => true,
));
// 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');
$friends = $facebook->api('/me/friends');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
<!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>
<fb:login-button autologoutlink="true" perms="email,user_birthday" size="large" onlogin="window.location = 'youlike.php';">Connect to this application using Facebook</fb:login-button>
<?php if ($user): ?>
<h3>Vous</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<pre><?php print_r($user_profile); ?></pre>
Date de Naissance : <?php echo $user_profile["birthday"];
echo '<ul>';
foreach ($friends["data"] as $value) {
echo '<li>';
echo '<div class="pic">';
echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture"/>';
echo '</div>';
echo '<div class="picName">'.$value["name"].'</div>';
echo '</li>';
}
echo '</ul>';?>
<?php endif ?>
</body>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
</div>
<script>
FB.init({appId: '453976824647366', status: true,
cookie: true, xfbml: true});
</script>
</div>
</html>
You can always force a refresh on login/logout, just to be sure:
after your FB.init
FB.Event.subscribe('auth.login', function(){
window.location.reload();
});
FB.Event.subscribe('auth.logout', function(){
window.location.reload();
});
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.