I have a file named fbmain.php which do Authentication part of my facebook app. But it doesn't display the Authentication window?
//fbmain.php
<?php
//facebook application
//set facebook application id, secret key and api key here
$fbconfig['appid' ] = "MY_APP_ID";
$fbconfig['secret'] = "MY_APP_SECRET";
$uid = null; //facebook user id
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
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update,user_birthday,user_location,user_work_history,user_likes,user_photos'
)
);
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
if ($session) {
echo "<br/>session ok"; //It create a session and disply 'session ok' string
try {
$uid = $facebook->getUser();
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
}
?>
Can anyone please help me with this?
Which version of PHP-SDK you are using??
I think you can get rid of getSession() and use just getUser().
change your code as below and try again..
$uid = $facebook->getUser();
if ($uid) {
// do some api calls or something else
}
else
{
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
else in index.php just include your fbmain.php and use a image button where you can link the $loginurl..
Related
I'm implementing Facebook login on my website, it's working without error but ultimately I'm not able to get any information of a user.
I've following code.
<?php
include 'library.php';
include 'facebook.php';
$app_id = "XXXXXXXXX";
$app_secret = "XXXXXXXXX";
$site_url = "XXXXXXXXX";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
$user = NULL;
}
}
if($user){
$logoutUrl = $facebook->getLogoutUrl();
}else{
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
if($user){
Echo "Email : " . $user_profile['email'];
}
$user_fbid = $fbuser;
$user_email = $user_profile["email"];
$user_fname = $user_profile["first_name"];
$user_image = "https://graph.facebook.com/".$user_fbid."/picture?type=large";
echo $loginUrl = $facebook->getLoginUrl( array('scope' => 'email,read_stream'));
What's wrong with this? Can anyone point out? I've gone though a number of questions but none of them seemed to work for me & I finally ended up posting a question myself.
EDIT
When asking for login, it displays permissions correctly like email, friend list, your likes etc. So basically it's asking correctly & even though I approve, information is not coming up. Tried with a couple of users.
EDIT
Try use this code, and work from there ... That's how I figured it out
<HTML>
<BODY>
<?php
require_once("fb/facebook.php");
$config = array(
'appId' => 'XXXXX',
'secret' => 'XXXXXXX',
'fileUpload' => false, // optional
'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$uid = $facebook->getUser();
$loginParams = array('scope' => 'email');
$loginUrl = $facebook->getLoginUrl($loginParams);
$logoutParams = array('next' => 'http://fb_logouturi');
$logoutUrl = $facebook->getLogoutUrl($logoutParams);
echo $uid . '</br>';
echo 'Login:' . $loginUrl . '</br>';
echo 'Logout:' . $logoutUrl . '</br>';
if($uid) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$user_profile = $facebook->api('/me','GET');
echo "</br>First Name: " . $user_profile['first_name'];
echo "</br>Last Name: " . $user_profile['last_name'];
echo "</br>Email: " . $user_profile['email'];
echo "</br>Gender: " . $user_profile['gender'];
echo "</br><img src='http://graph.facebook.com/" . $uid ."/picture?type=large'>";
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</BODY>
</HTML>
I m using the facebook sample javascript code that come with sdk but no luck
i am using the latest 3.2.2 SDK
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '---------',
'secret' => '----------',
));
// See if there is a user from a cookie
echo $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;
}
}
Use this code:
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '---------',
'secret' => '----------',
));
$querystr=http_build_query($_REQUEST);
// See if there is a user from a cookie
$user = $facebook->getUser();
if(isset($_REQUEST['code'])) {
if(!empty($_REQUEST['code'])) {
setcookie("access_token", $_REQUEST['code'], time()+120);
}
echo ("<script> top.location.href='<YOUR APP URL>'</script>");
exit;
}
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;
}
}else {
$scope='email';
$params = array(
'redirect_uri' => 'YOUR SITE URL(WHere app is hosted)',
'display' => 'page',
'scope' => $scope
);
$loginurl = $facebook->getLoginUrl($params);
echo ("<script> top.location.href='".$loginurl."'</script>");
exit;
}
I have this script:
This code should post a Text and a Link to a WebSite
<?
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('facebook.php');
$c = array(
'appId' => '4102137023*****',
'secret' => '*****c4a60cb08*****7c0333*****',
);
$facebook = new Facebook($c);
$uid = $facebook->getUser();
echo "Userid: " . $uid;
echo "<BR>";
?>
<html>
<head></head>
<body>
<?
if($uid){
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'angel-craft.de',
'message' => 'Wenn ihr das hier seht freut euch auf ein Game'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
echo $e->getType();
echo $e->getMessage();
}
// Give the user a logout link
echo '<br />logout';
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
?>
</body>
</html>
This should post a link with a text
but the Userid($uid) stays empty.
And YES this is the Demo script from FB dev.
I've used this successfully.
<?php
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '-----',
'secret' => '-----',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$Name = $me['first_name'];
$properties = array(
array(
'text'=>'Property Link',
'href'=>'http://www.yahoo.com'
),
array(
'text'=>'Property Link',
'href'=>'http://www.yahoo.com'
)
);
// Link that is adjacent to "Like" and "Comment" at the very bottom of the post.
$action_links = array(
'name'=>'Test',
'link'=>'http://www.yahoo.com'
);
// Dictates who can see the post.
$privacy = array(
'value'=>'ALL_FRIENDS'
);
// api('/me/feed', 'post',... = Wall Post.
$wallPost = $facebook->api('me/feed', 'post', array(
'message'=> 'Testing',
'link'=> 'http://www.yahoo.com',
'properties'=>$properties,
'actions'=>$action_links
)
);
} catch (FacebookApiException $e) {
error_log($e);
}
}
$par = array();
$par['req_perms'] = "email, publish_stream";
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl($par);
}
?>
I found the bug, The cert was old. Thanks to all :)
I'm using the Facebook openGraph API for PHP. Here is my code:
<?php
session_start();
if (!isset($_SESSION['username'])) {
header("Location: index.php");
break;
}
$fbconfig['appid' ] = "APPID";
$fbconfig['secret'] = "SECRET";
$fbconfig['baseurl'] = "http://blah.com/";
$user = null;
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
try {
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
} catch (Exception $e) { echo "ERROR: ".$e; }
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,read_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri' => $fbconfig['baseurl'].'blah.php'
)
);
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => ($fbconfig['baseurl'].'blah.php') ));
//if user is logged in and session is valid.
if ($user ){
header("Location;$loginUrl");
break;
}
?>
Now, this works on other pages (the Facebook part) but on this page only (same dir as other pages) The browser thinks that the PHP ends at "'appId' =>" and from then on interprets it as html. So I get this in my browser:
http://cl.ly/image/1y2a2F0e3I35
Sorry for the weird cropped picture, but I can't divulge the name or intent of the business.
There is HTML after the PHP on the real site and I can give this + js if needed.
Thank you!
your header is wrong.......try following......
header("Location:$loginUrl");
and also if you don't get facebook user id then you have to redirect to login page but you did reverse.may do following
//if user is not loggin
if (empty($user)){
header("Location:$loginUrl");
}else{
try {
$userInfo = $facebook->api("/$user");
} catch (FacebookApiException $e) {
$userInfo=null;
}
}
I am trying to get if user is logined in facebook, if yes then I need to store their profile pic url, name in my db, so that users can comment in my web application.
I cant do this, I created a facebook application for website and using the following code for auth purpose. But its showing "An error occurred. Please try again later."
Any one please help me ? or help me with working code ?
<?php
require_once('../src/facebook.php');
$fb_app_id = "key";
$fb_secret = "secret";
$fb_app_url = "url";
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
// The user is logged in
try {
$user_profile = $facebook->api('/me');
// Here : API call succeeded, you have a valid access token
} catch (FacebookApiException $e) {
// Here : API call failed, you don't have a valid access token
// you have to send him to $facebook->getLoginUrl()
$user = null;
}
} // else : the user is not logged in
?>
<?php if ($user): ?>
Logout of Facebook
<?php else: ?>
Login with Facebook
<?php endif ?>
You can try this
require_once('../src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxx',
'cookie' => true,
));
$fb_user_id = $facebook->getUser();
try
{
$fb_user_profile = $facebook->api('/me');
$me = $facebook->api('/me');
}
catch (FacebookApiException $e)
{
$fb_user_id = NULL;
}
$fbid = $me['id'];
$firstname = $me["first_name"];
$lastname = $me["last_name"];
$gender = $me["gender"];
$email = $me["email"];
if ($facebook->getSession()) {
echo '<div id="fbc-img"><img src="http://graph.facebook.com/'.$me['id'].'/picture?type=normal" width="56" height="56"></div>
<div id="fbc-info">Welkom, '.$firstname.' ! <br>Logout </div> ';
} else {
echo 'Login met facebook ';
}