$facebook->getUser() returning 0 using PHP SDK - facebook

I have just started with the Facebook application development. I downloaded the PHP SDK from Facebook couple of weeks ago.
Following is the first piece of code which i wrote.
<?php
require 'src/facebook.php';
$app_id = 'My APP ID';
$application_secret = 'My APP Secret';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $application_secret,
'cookie' => true, // enable optional cookie support
));
$uid = $facebook->getUser();
echo $uid;
?>
But it is always giving me 0 as a result even when i am already login into my facebook account in other tab.
My Canvas URL is pointing to my localhost as i have hosted this app on my local machine only.
I have read almost every post related to this issue but i was not able to solve this issue somehow.. It would be really helpful if anybody could help me in resolving this issue.
Cheers.
Ajay

The code will always return 0 unless you have authorised the app - even if you are logged into facebook elsewhere. Login to your app by going to the URL outputted by $facebook->getLoginUrl();
Then, you will find that $facebook->getUser() will return the correct User ID.

Related

Unable to get userid using Facebook PHP SDK

I am building a Facebook app and need to get the userid directly from Facebook. I intend to use Facebook PHP SDK to get the userid but it always crash my server.
Here's my code:
<?php
require 'facebook-php-sdk-master/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
// Get User ID
$user = $facebook->getUser();
echo $user;
?>
I always get the server error message:
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
I'm using the Facebook PHP SDK from https://github.com/facebook/facebook-php-sdk. Can someone help me with regards to this problem?
I found out the error. It turns out that there's an error in the base_facebook.php file. You need to modify the getCurrentUrl() function to redirect to the app URL that you used in the App Settings. Hope that save someone from debugging for hours!

Facebook PHP sdk doesnt set cookie with iframe linking

I am using PHP sdk for Facebook.
require_once 'facebook-sdk/facebook.php';
session_start();
$facebook = new Facebook(array(
'appId' => 'app_id',
'secret' => 'app_secret',
'cookie' => true,
));
$facebook->getUser();
if($user){
// do stuff
}else{
//redirect to app login
}
After this piece of code there is link on my page that points to this same page with a query string.
something like Continue
But when I click this link $user is "0". This happens only şn Safari and IE. When I check cookies set by app doesnt exist in these browsers.
Any ideas?
Since the user id won't change, is it a problem if you send it via GET?
Continue
and then at the top of your script and after getting the $user the way you are doing:
if($_GET['step'] == 2){
$user = $_GET['id'];
}
If you really need to keep the cookies you can add this at the top of your script, it must be the first line.
header('P3P: CP="CAO COR CURa ADMa DEVa OUR IND ONL COM DEM PRE"');
Also you might want to check this questions
Iframe Facebook application and cookies [Internet Explorer]
Session Lost on IE Facebook App iFrame
wich are related to the header solution.

how to tell if the user is already logged in and has already accepted my app

I am just creating a facebook tab on a users page.
I am using the php facebook SDK, and this little snippet(see below) from the tutorials.
My question is , if the user is logged into FB and comes back to my page later $user is not found and they have to connect again. Is there not a way to know that they are already logged in and already granted me access?
I hope this question makes sense
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_APP_SECRET,
'cookies' => 'true'
));
$user = $facebook->getUser();
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'publish_stream, manage_pages'));
}
Nope, it won't be done automagically on server-side. To know who user is - you need to ask them to follow the login url
OR
use JS SDK. In that case user is logged in automatically and all you need is to refresh the page by JS (there is an example shipped with php sdk on how to do that)

Upload to specific Fan Page Album without user perms

I'm trying to replicate the same functionality as someone else has already achieved on this page here:
http://www.facebook.com/PowerPhotoUploader?sk=app_152884604799537
am not bothered about the forced like fangate part, can do that no probs.
I need to achieve this without requesting any user perms the same way they have
I've got close, but not quite right yet.
Have created a test album on this page: http://www.facebook.com/pages/Demo-Album-Upload/227979503931257?sk=app_153866511376478
The code I have is uploading my specified image, however it is ignoring the album id I input and instead, uploading to an album on my own profile.
Code so far is:
<?php
$app_id = "XXXXXXXXXXX";
$app_secret = "XXXXXXXXXXXX";
require 'facebook.php';
$facebook = new Facebook(array( 'appId' => $app_id, 'secret' => $app_secret, 'cookie' => true, 'fileUpload' => true,));
$signed_request = $facebook->getSignedRequest();
//print_r ($signed_request);
$page_id = $signed_request["page"]["id"];
//Upload To Page Album
$facebook->setFileUploadSupport(true);
$album_id ='59125';
$file_path ='image2.gif';
$args = array('message' => 'Photo Caption');
$args['image'] = '#' . realpath($file_path);
$data = $facebook->api('/'.$album_id.'/photos', 'post', $args);
print_r($data);
?>
Have already read through a lot of forum material, have set the filuploadsupport, set file upload to true, but most of the info I can find so far reuires perms & access token, however sample above has managed to achieve with neither - any thoughts?
Regards Tony
Tony the trick here is that you only need one access token, not a new one for each user. The idea is that the user is not posting to the page, you are posting to the page, so you do not need an access token from the user. However since this is a secure call to Facebook you still need to provide an access token that has the ability to publish to the page.
The simplest route to get an access token that you can use would be to manually give the application the manage_pages and offline_access permissions for your account. Then just grab the access token for for your account and use it for all calls.

URL for testing my app on facebook

I'm just starting a new app with the php-sdk. I've done an app a few years ago, but this is the first time with the newer setup dialogs.
The canvas url is pointing to my web server and the app's subdirectory.
Right now this is the only code in my app...just the "hello world" from the php-sdk sample.
<?php
include 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'myappid',
'secret' => 'mysecret',
));
// Get User ID
$user = $facebook->getUser();
?>
Yes, the appid and secret are the actual numbers.
I've waited several minutes to propagate but when going to https://apps.facebook.com/myappsname it just tosses me a 404 error. Is there another URL I should go to when it's in sandbox?
To get https://apps.facebook.com/[APP_NAMESPACE] working, you must specify [APP_NAMESPACE] the App Namespace in the Dev App (https://developers.facebook.com/apps) under Basic settings.