I'm trying to retrieve Facebook user's information using Facebook api in PHP. But when I try to run the following code, the no content is displayed, especially because of the require statement. The HTML content on this page is not rendered. In other words, nothing works. Only the echo above require statements executes. I have correct APP ID and Secret Code.
<?php
session_start();
?>
<?php
$somevar=$_SESSION['newsession'];
$somevar2=$_SESSION['newsession2'];
echo "rahul fb";
require 'fblib/src/Facebook/Facebook.php';
$facebook = new Facebook(array(
'appId' => '
myappid',
'secret' => 'mysecretid',
));
// Get User ID
$user = $facebook->getUser();
To make API calls:
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
echo "SFDFFD";
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}else{
echo "fatal error";
}
//echo "sdfdsfdf";
?>
What can be the issue? Am I doing it in right way? or is there any other way?
You need to require the autoload.php to make it work.
Also change the syntax:
$facebook = new Facebook(array(
'appId' => '
{1821595718085107}',
'secret' => '{d17998fdecdeab3c158970c1fcbf89f4}',
));
Related
I am using php sdk for facebook login on my website. But i am facing a very stupid problem, when i click on connect facebook button it redirects me to facebook website to enter email and password but after redirecting back to my website it creates infinite loop which does not end.
This is my code:
require('facebook/facebook.php');
$facebook = new Facebook(array(
'appId' => 'app_id',
'secret' => 'app_secret',
));
// See if there is a user from a cookie
$user = $facebook->getUser();
//echo $user;
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
print"<pre>";
print_r($user_profile);
print"</pre>";
You can test the it on this url:
http://www.conceptbeans.co.uk/cb/projects/xedact/login.php
I will be appreciable any help from your side.
Thanks
Zain
your syntax isnt correct when you define your new facebook object, you have a , at the end of array. should be:
$facebook = new Facebook(array(
'appId' => 'app_id',
'secret' => 'app_secret'
));
Every time I run this script it gives me a different access token. I thought the access token was unique to a user.. Also why is this script so unreliable, fails half the time.
My Facebook Access Token Page
Here is the code:
<?php
require 'lib/facebook.php';
require_once('C:\inetpub\storeboard.com\linkedin\extract_data.php');
$facebook = new Facebook(array(
'appId' => FACEBOOK_APPID,
'secret' => FACEBOOK_APP_SECRET
));
$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) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
$access_token = $facebook->getAccessToken();
if (isset($access_token)) {
echo $access_token;
//header( 'Location: http://www.'.APP_ROOT_DOMAIN.'/facebook/save_token.asp?nToken='.$access_token);
}
?>
Have I cut out code that is required from the original: http://developers.facebook.com/blog/post/534/
or am I missing something.
require_once('C:\inetpub\storeboard.com\linkedin\extract_data.php');
This line of code just extracts the APPID and APPSECRET from the database.
I am a complete beginner when it comes to PHP, so any help would be greatly appreciated.
Many Thanks!
You don't need to play with the token to get simple authentication:
After you get facebook api instance:
$facebook = new Facebook(array(
'appId' => FACEBOOK_APPID,
'secret' => FACEBOOK_APP_SECRET
));
If you are using me API - get the user profile:
$user_profile = $facebook->api('/me');
then obtain email from it:
$email = $user_profile['email'];
At this point you check if the email is valid in your database - if yes start the user session and you are good to go.
Is it possible to post to a friends wall/timeline in facebook using an app even if the user is not currently logged in? Here's the code that I'm currently working on, as you can see it posts a message on a friends wall(substitute the friend_id with an actual profile id). This works but a user has to logged in in order to perform this operation.
I don't have any idea what to do if I want this script to execute automatically(without user intervention).
<?php
require 'php_sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
));
$session = $facebook->getUser();
$me = null;
if($session){
try{
$me = $facebook->api('/me');
$facebook->api('/friend_id/feed', 'post', array('message'=>'hello without user!'));
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}
if($me){
$logoutUrl = $facebook->getLogoutUrl();
echo "<a href='$logoutUrl'>Logout</a>";
}else{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_about_me,user_birthday,user_location,email,read_friendlists,friends_location,friends_birthday,publish_stream'
));
echo "<a href='$loginUrl'>Login</a>";
}
?>
Any ideas? Code samples, and links to specific documents that can help me gain a bit of idea on how this works is greatly appreciated thanks!
When the user is logged in, you need to trap their "Access Token"
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
));
// NOTE: wrap these in try/catch blocks for safety - this is example only
$session = $facebook->getUser();
$access_token = $facebook->getAccessToken();
To post as them later, you use the access token
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
));
// NOTE: wrap these in try/catch blocks for safety - this is example only
$facebook->setAccessToken($access_token);
// Then check /me and you should still have the same user.
// Then post to /me/feed to post to the wall.
The token will be valid for 2 days. You can extend this to 60 days if you want.
Edit: For exenteding the token, you call the function
$facebook->setExtendedAccessToken();
immediately before "getAccessToken" call.
I've recently been trying to get my app working with the PHP SDK 3.0 and been having some trouble with it seeing that I am actually logged in. I have the "example.php" from the 3.1.1 download that doesn't acknowledge that I'm logged in. The file "with_js_sdk.php" also doesn't acknowledge that I'm logged in when you first run it. After it has been run, however both that file and the "example.php" will see that I'm logged in. Once I log out, "example.php" doesn't work and "with_js_sdk.php" fails on the first run, but all subsequent runs both files (and some other files I'm testing this with) will all authenticate correctly. When not working, all files give an "OAuthException" with the message: "An active access token must be used to query information about the current user." even when I'm logged in. So my question is, is there something in the "with_js_sdk.php" that I can set in my other files? I'm guessing it has something to do with the "oauth:true" in that file from what I've read, although I'm not a Facebook coding expert so I'm not sure. And I have an iframe app, so I don't have the "FB.init" part in my application (that worked incidentally until the change in the sdk). Here's the minimum code I'm testing that's giving the error:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '11111111111111111',
'secret' => 'abcdefghijk etc...',
'cookie'=>true
));
// See if there is a user from a cookie
$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) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
?>
The getuser always comes back correctly, but the $facebook->api('/me') call always fails until I run a file with the fbml in it. And before you ask, yes I changed the appID and secret in the files. Thanks.
Darryl
Instead of me pass $user. Eg:
$user_profile = $facebook->api('/'.$user);
I had the same problem and suffered for the longest time, and solved it by simply getting the new PHP SDK files from https://github.com/facebook/php-sdk.
Reference: http://developers.facebook.com/docs/reference/php/
Have you tried print_r($user_profile) to see what comes out? Also, the cookie parameter is not used with the latest SDK.
You have to be aware of the scope of variables, $user_profile will be only inside the scope of you if statement, so If you want to access from outer scope it will be not available remember you can always declare your variable outside and then initialize it, this way it will be available through your php file.
?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '11111111111111111',
'secret' => 'abcdefghijk etc...',
'cookie'=>true
));
// See if there is a user from a cookie
$user = $facebook->getUser();
$user_profile; // variable being declare outer scope
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
?>
THIS WORKS FOR ME
$user_profile = $facebook->api('/'.$user);
And here is my full php fb connect script example: `
$fbconfig['appid' ] = " id ";
$fbconfig['secret'] = " secret ";
$fbconfig['appBaseUrl'] = "https://apps.facebook.com/ example /";
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
//Facebook Authentication part
$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.
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown'
)
);
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$me = $facebook->api('/'.$user);
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e); // d is a debug function defined at the end of this file
$user = null;
}
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
//get user basic description
$me = $facebook->api("/$user");
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
if (isset($_GET['code'])){
header("Location: " . $fbconfig['appBaseUrl']);
exit;
}`
Have a nice day ! :)
when the token is invalid, kick off the authentication flow.
also, to get jssdk to work with php sdk v3.x, make sure you have upgrade your js sdk code to use OAuth 2.0.
you are missing with something.To have an active access token you must present it inside a session variable.And you are not even creating a session in your code.
YOU MUST HAVE AN ACCESS TOKEN FIRST
try this->
$access_token =$facebook->getAccessToken();
if ($user) {
if( session_id() ) {}
else {session_start();}
Now you should store $_SESSION['access_token']=$access_token ; for later use.
Now I'm developing a mobile web application using facebook connect, but I am having problems with the OAuth Dialog. I use the following facebook documents for my reference:
http://developers.facebook.com/docs/guides/mobile/#web
I choose to use the OAuth Dialog instead of the Login Button because mobile browsers do not recognize the FBML (I use the Blackberry browser on testing). The OAuth Dialog also lets me add a list of permissions within the parameters of scope. But the problem is when I've logged in using the OAuth Dialog, the parameter $me wasn't recognized so that the login button still appears and doesn't not change to the logout button.
Here is an example of my code:
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$accessToken = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
error_log($e);
}
}
if ($me) {
echo "<img src=\"images/logoutFB.gif\">";
} else {
echo "<img src=\"images/loginFB.gif\">";
}
Is the parameter $me can't be used if I use the OAuth Dialog to connects to facebook?
So how do I know that I am already logged into facebook or not if I use the OAuth Dialog?
Please help if you guys have the solution.
Looks like you need to use the updated PHP-SDK
getSession() has been replaced by getUser():
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => '135669679827333',
'secret' => 'xxxxxxxxxxxxxxxx',
));
$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) {
$user = null;
}
}