Im a totally facebook newbie developer.This is the sdk version that i use: click . This is the login part:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXx',
'cookie' => true
));
?>
<?php
$loginUrl = $facebook->getLoginUrl(array (
'scope' => 'email',
'redirect_uri' => 'http://facebook.ebis-servicii.ro/mytest/test.php'
));
$user = $facebook->getUser();
echo 'Login Here ';
?>
And this is the part where I want to get some informations about the user:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXX',
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try{
$user_profile = $facebook->api('/me');
var_dump($user_profile);
$fbid = $user_profile['id']; // To Get Facebook ID
$fbuname = $user_profile['username']; // To Get Facebook Username
$fbfullname = $user_profile['name']; // To Get Facebook full name
$femail = $user_profile['email']; // To Get Facebook email ID
}catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
echo "FBID ".$fbid."<br>";
echo "Email ".$femail."<br>";
echo "Name ".$fbfullname."<br>";
echo "Username ".$fbuname."<br>";
echo "Facebook Picture:<br>";
//echo '<img src="https://graph.facebook.com/'.$_SESSION["USERNAME"].'>;
echo "<img src ='https://graph.facebook.com/".$fbuname."'>";
}
?>
This is the url link where you see the results. So my question is how do I get the username? In the result of the var dump there is no username .
Since v2.0, the username is not included in the result anymore, as you can see in the Facebook docs: https://developers.facebook.com/docs/graph-api/reference/v2.1/user
Else, the App Scoped IDs would be pointless, and they came with v2.0 too. Apps should not be able to get to the "real" profile of the users.
You should be able to use the App Scoped ID to show the user picture:
echo '<img src="https://graph.facebook.com/'.$fbid.'/picture?width=121&height=100" />';
Btw, i would suggest using the new PHP SDK (4.x) for new Apps.
Related
Years ago I've developed apps for Facebook, but now seems not working old method to get user's information (id, name, etc) and post data on user's wall.
I've used like this, but for now It returns me blank screen:
require_once('FacebookAPI/Facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
));
$user = $facebook->getUser();
if ($user)
{
try
{
#Code below to post data on user's wall
# Photo Caption
$photoCaption = 'Just used app';
$imageUrl = ('http://example.com'); // Example URL
$link = ('http://www.facebook.com/page/app_xxxx?ref=ts');
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'link' => $link,
'caption' => 'Caption text'
);
$apiResponse = $facebook->api('/me/feed', 'POST', $post_data);
?>
#Code below to show app
<html>
<body bgcolor="#ffffff">
<center>
// link to my app
</center>
</body>
</html>
// Code below to get user's information (name, id)
<?php
$user_profile = $facebook->api('/me');
$coded = $_REQUEST['code'];
$name = $user_profile['name'];
$id = $user_profile['id'];
session_start();
$_SESSION['name'] = $name;
$_SESSION['id'] = $id;
}
catch (FacebookApiException $e)
{
$user = null;
error_log($e);
}
}
else
{ # Code below to get access from user
$redirectUri = 'http://www.facebook.com/mypage/app_xxxx?ref=ts';
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload',
'redirect_uri' => $redirectUri
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
Could someone help me out to get successfully user's name, id?
You can try this code. Hope you have downloaded latest php-sdk, you have appid,app des
include_once "php-sdk/src/facebook.php";
$app_id = 'your app id';
$facebook = new Facebook(array(
'appId' => 'your app id',
'secret' => 'your app secret'
));
$user = $facebook->getUser();
if ($user) {
$me = $facebook->api('/me');
$name= $me['name'];
$fid=$me['id'];
}
else
{
$loginUrl = "http://www.facebook.com/dialog/oauth?client_id=" .
$app_id . "&redirect_uri=" . urlencode($app_url) . "&scope=email";
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
I want to get user profile using access token that i have already generated, and if the access token is invalid it shoud display error. anyone can help to do this ? i want php code to handle this. i am waiting....
Since you already have the access token, the simplest and clean way would be-
$user_details = "https://graph.facebook.com/me?access_token=" .$access_token;
$response = file_get_contents($user_details);
$response = json_decode($response);
print_r($response);
First check if the user is logged in
$facebook = new Facebook(array(
'appId' => 'AppIDHere',
'secret' => 'AppSecretHere',
));
// 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;
}
}
If your access token is not null, fetch user data
<?php if ($user): ?>
Profile Picture: <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
User Id: <?php echo $user_profile['id'];?>
Name: <?php echo $user_profile['name'];?>
First Name: <?php echo $user_profile['first_name'];?>
Last Name: <?php echo $user_profile['last_name'];?>
Link: <?php echo $user_profile['link'];?>
Gender: <?php echo $user_profile['gender'];?>
Username: <?php echo $user_profile['username'];?>
<?php else: ?>
<strong>You are not Logged In.</strong>
<?php endif ?>
The user variables can be got from Facebook Graph API
If You want to get user_email and other info then you can use this code....
$facebook = new Facebook(array(
'appId' => 'AppIDHere',
'secret' => 'AppSecretHere',
));
// 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?fields=email,name,id,gender');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
I used facebook graph Api
$facebook = new Facebook(array('appId' => $fbconfig['appid'],'secret' =>
$fbconfig['secret'],'cookie' => true,));
$session = $facebook->getUser();
if($session)
{
try{
$uid = $session;
$fbme = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
// FQL : to fetch the email id
$fql = "SELECT email FROM user where uid='".$uid."'";
$response = $facebook->api(array('method' =>
'fql.query','query' =>$fql,));
$user_email = $response[0][email];
// Friend COunt.
$friend_res = $facebook->api('/me/friends');
$friend_count = count($friend_res[data]);
//likes
$likes = $facebook->api('/me/likes');
$fb_like = $likes[data];
// Getting more user personal data
$param = array(
'method' => 'users.getinfo',
'uids' => $uid,
'fields' => 'name,sex,pic,current_location,profile_url,email,birthday',
'callback'=> ''
);
$userInfo = $facebook->api($param);
}catch(FacebookApiException $e){
}
}
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>
How can i check if the current facebook user like a facebook page?
I have this code,
require_once("/path/to/sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$user = $facebook->getUser();
if ($user) {
try {
$likes = $facebook->api("/me/likes/153649677989200");
if( !empty($likes['data']) )
echo "I like!";
else
echo "not a fan!";
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
}
But is not responding if the user like the page, please see the page here
Thanks!
You can use FQL
$isFan = $facebook->api(array(
"method" => "fql.query",
"query" => "SELECT uid FROM page_fan WHERE page_id = '<YOUR PAGE ID>' AND uid = '<UID>"
));
I don't know if you need to do this only using FQL, but there is another easier way:
require_once("/path/to/sdk/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'APP_ID',
'secret' => 'APP_SECRET',
));
$signed_request = $facebook->getSignedRequest();
$like = $signed_request['page']['liked'];
$like will be empty or 1.
I like to check variables first, like this:
if(is_array($signed_request) && isset($signed_request['page']) {
if($signed_request['page']['liked']) {
// do whatever you want to do if the user likes the page
} else {
// do whatever you want to do if the user don't like the page yet
}
}
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 ';
}