Can't get fb user's information - facebook

I'm getting started with Fb app development. I tried to deploy the example app given in the facebook php api but don't seem to get it working
Here is the code:-
<?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();
}
$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/aitik/picture">
<?php echo $naitik['name'];
?>
</body>
</html>
But $user is always null, and it shows "You are not connected".
Is it because I'm using a free webhost and not having any secured URL?
Url of my app, if you want to see: http://apps.facebook.com/iiiimags

Related

Facebook login - php sdk - prob

I am in a total mess with facebook-login. The code is given below. You can see the output at http://beta.jokesnfunnypics.com/login. In the scope, I have given as email but when I click the link and is redirected to facebook, it says that the app asks for only the basic permissions. Moreover, I have mentioned the redirect_uri as http://beta.jokesnfunnypics.com/add but I am instead redirected to the same page only. More over again, after getting the permission, no info is being displayed at the page also. Please help me. I am ready to give a few dollars to anyone who gives me the correct code.
<?php
//Application Configurations
$app_id = "XXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXX";
$site_url = "http://www.beta.jokesnfunnypics.com/add";
try{
include_once "src/facebook.php";
}catch(Exception $e){
error_log($e);
}
// Create our application instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_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;
}
}
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
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>
</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>
$_SESSION['user']="abc;
<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>
From the code, $loginUrl variable is defined 2 times, scope and redirect_uri is missing in the second time . try to remove those lines from your code
<?php
$app_id = "XXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXX";
$site_url = "http://www.beta.jokesnfunnypics.com/add";
try{
include_once "src/facebook.php";
}catch(Exception $e){
error_log($e);
}
// Create our application instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_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;
}
}
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
</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>
$_SESSION['user']="abc";
<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>
Refer these tutorials .
Login with Facebook using PHP SDK
Login with facebook using PHP ( Demo and Download )
Well usually I don't use the php sdk to generate my login url, instead I do it manually, try changing this line:
Login with Facebook
To this:
Login with Facebook
The correct code in PHP - 3 steps in 1 form - is explained by iZend - The web engine, including the configuration on Facebook: http://www.izend.org/en/identification-by-facebook.
Opening a connection with Facebook using the SDK.
Obtaining a URL for a Connect with Facebook button.
Retrieving a connected user's email address (and other information like firstname and lastname for a register form).

Facebook php sdk - info not being shown

I downloaded the php sdk from github (link provided from developers.facebook.com). I uploaded the src folder at the root. Now, I pasted the code and replaced the appId and secret with mine. But the page is showing a server error. The following is my code :-
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$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();
}
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<title>php-sdk</title>
<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>
Please help me. I badly need some help. I cannot find any mistake nor have I modified any part of the code except the app id and the secret. The facebook.php file is in the src folder only. You can find my output at http://beta.jokesnfunnypics.com/login.
the issue is possibly that you are missing php Curl extension
Facebook PHP SDK Use's Curl To Send Requests.
so First you need to check if the CURL extension is installed at your server
like this
function _isCurl(){
return function_exists('curl_version');
}
if not install it and then it will all work

facebook user & friend permission not showing up on scopes

I have a simple login app to utilize oAuth for facebook.
I will need to access a few things about the user so i added a few parameters in theUser & Friend Permissions
I waited over 24 hours for the servers to propagate but when i debug the token, i dont see these permissions added to the scope
I grabbed the token by:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '**************',
'secret' => '**********************************'
));
// Get User ID
$user = $facebook->getUser();
$token = $facebook->getAccessToken();
echo $token;
if ($user) {
try {
$facebook->setFileUploadSupport(true);
$album = $facebook->api('/me/albums ');
print_r($album);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
<!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>
What am I missing?
any help please, thank you.
Changing needed permission after user installed make you need to ask for the new permission.
So you can uninstall and install again, or make a JS to check if your user have the necessary permission and, if not, ask using FB.ui

Facebook application instance creation not working for every users

I have the following issue that drives me crazy. I use the sample code published with the Facebook PHP SDK (https://github.com/facebook/php-sdk/) for identifying a user. It works fine for most of my test users but for some users, the instance creation does not work. I’ve tried with the same machine and different machines, it does not change anything. Each user is correctly set as test user in my dev account.
What could be the cause?
Many thanks in advance.
require '../lib/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXX',
'secret' => 'XXXXXXXXXXX',
));
// 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(
'scope' => 'publish_stream, publish_actions',
'redirect_uri' => 'http://apps.facebook.com/xxxxx/'
));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
</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>
I finally found the answer. The problem was not related to the code. It was due to the fact that the test users bypassed the notification requesting to accept the invitation and instead entered directly the app url.
You have to delete the test user and recreate it. Then the test user will recieve a new notification and will then have to accept it.

Facebook Connect Fetching user Data

I have followed the Facebook PHP SDK example and created the following.
<?php
require ("lib/facebook.php");
$facebook = new Facebook(array('appId' => 'XXXXXXXXXXXX', 'secret' => 'XXXXXXXXXXXXXXXXXXXXXXX', 'cookie' => true, ));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
}
catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if($me) {
$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>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php
echo $facebook->getAppId();
?>',
session : <?php
echo json_encode($session);
?>, // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<?php
if($me):
?>
<a href="<?php
echo $logoutUrl;
?>"> <img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif"> </a>
<?php
else:
?>
<div> Using JavaScript & XFBML:
<fb:login-button></fb:login-button>
</div>
<?php
endif;
?>
<h3>Session</h3>
<?php
if($me):
?>
<pre><?php
print_r($session);
?></pre>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php
echo $uid;
?>/picture"> <?php
echo $me['name'];
?>
<h3>Your User Object</h3>
<pre><?php
print_r($me);
?></pre>
<?php
else:
?>
<strong><em>You are not Connected.</em></strong>
<?php
endif;
?>
<?php
print_r($me);
?>
</body>
</html>
When i click Login. It opens the window and when i allow permission. It refreshes the window. only to show the same content. when i do a print_r($me) nothing is visible.
When i check in the Application settings. The application has been installed in my fb account.
EDIT
Check that the Canvas URL you have set up into your app configuration is the same that the one which you are executing the code.
If it's not the same, this is a common behaviour.
i think the canvas url is not configured well as per your application .
you have to configure it,otherwise it is the common phenomenan.