Cant get users posts with facebook php sdk - php

When I try to get the users posts it just shows "Array ( [data] => Array ( ) )" (the profile shows and I can post to the wall). It works when I change "me" to a public page. The Login-link and user-info shows. Please help me, I don't get why it doesn't work!
<?php
session_start();
require 'facebook/facebook-php-sdk/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxx',
));
// Get User ID
$user = $facebook->getUser();
if ($user)
{
$url = $facebook->getLogoutUrl();
}
else
{
$url = $facebook->getLoginUrl();
}
if ($user) {
try {
$pageFeed = $facebook->api('me' . '/feed');
print_r($pageFeed);
// 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!
<pre><?php print_r($user_profile); ?></pre>

You need the read_stream permission on Login.
so you need to replace your getLoginUrl() call with the following.
You could also add more permissions separated by comma,
Ex: 'read_stream,publish_stream,etc...'
$url = $facebook->getLoginUrl(array('scope' => 'read_stream'));

Related

PHP Facebook SDK doesn't work and returns code

My PHP Facebook SDK is not working and always returns $user=0 Why?
It is my first time to deal with facebook sdk
my code:
require './facebook-php/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxx'
));
// 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) {
$logoutUrl = $facebook->getLogoutUrl(); echo 'there is a user';
} else {
$loginUrl = $facebook->getLoginUrl();
echo 'there is noo user <a href='.$loginUrl.'>login</a>';
}
below is the url return code :
http://localhost/e-commerce/testit.php?code=AQA8DckDMqNarptbCUIfAmn2tIyn1mjVBmHTjOMaQ4GqsFW_6vqemGW35TC07Kxq7kuB4QkKibHJ2-m9GntIx0biWEiR_FUuozsONvgc_tNmtGURpcm78DnIE26vT6hbjz10eZECuh-GLec9tGplhLrDNbLO-aX2Hv2Mu-8CQhAdiFQ6AYW3Nt09a8Rzahbn35Jz0cNyB_G7Ksjorq3iSs6Bpa6Tk0d7Vx0A0f1Mg77PgD6IuN_grjkNHDWmb_ywoqW48IqoxJUhnRmL3qi1G-CT7ft_f77rRr-VP-yQ-LTkdNja0rBzoFnFGccMuQ86u8tk7nfnx5qrSLBpHLEIBqQT&state=84ff9f36e8de53d13c96bcf7cb2da3b3#=
I have uploaded Facebook setting image
Facebook settings images

How to get user profile info using access token in php-sdk

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){
}
}

PHP detect if user is fan of page doesn't always work

I'm using the following code as part of my facebook application that get user's permissions (mainly : likes ). This code will detect if the user is a fan of a page or not and based on that it will show some content or redirect him to another page, if he is not a fan. The main problem is that it works only one time or two times and then it shows nothing.
http://pastebin.com/RxefpmzD
<?php
require 'lib/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
));
$user = $facebook->getUser();
if ($user) {
try {
$likes = $facebook->api("/me/likes/333605930105938");
if( !empty($likes['data']) )
echo "<iframe width=\"745\" height=\"535\" src=\"http://www.youtube.com/embed/".$donnees['link']."?fs=1&autoplay=1&loop=1\" frameborder=\"0\" allowFullScreen=\"\"></iframe>";
else
echo "<iframe width=\"745\" height=\"535\" src=\"scren.php?id=".$donnees['id']."\" frameborder=\"0\" allowFullScreen=\"\"></iframe>";
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
}
?>

$facebook->api('/me') not giving the user data

require_once($_SERVER['DOCUMENT_ROOT'] . '/facebook-php-sdk/facebook.php');
$facebook = new Facebook(array(
'appId' => FB_MAIN_APP_API_ID,
'secret' => FB_MAIN_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;
}
}
Above is the code I'm using to get the Facebook details for my application. In my localhost this is working fine and it gives my Facebook email, user id. After I upload this in to live server this function is not working and it's not giving my Facebook details. I checked on the Facebook app id and it is the correct one.
I cannot figure out why it's not working in live site since it is working in my localhost. Please help me with this. Thank you.
I think problem is in your app which your are using on server. So try the same app which you are using for your localhost and in login and logout url instead of localhost use $_SERVER['HTTP_HOST'].
Example :
$session->fb_logout = $facebook->getLogoutUrl(array('next' => "http://" . $_SERVER['HTTP_HOST'] . "/path/to/logout"));
First check that what $user_profile = $facebook->api('/me'); showing. Just print_r the variable $user_profile.
Ex :
$user_profile = $facebook->api('/me');
print_r($user_profile);
exit;
if it showing blank then surely it will go in catch block. Then write following in catch block.
print_r($e);
exit;
For geting the user id, user need to grant permission
$user_id = $facebook->getUser();
echo $uid;
if($user_id){
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user_id = null;
}
}else{
$login_url = $facebook->getLoginUrl();
echo("<br>login url=".$login_url);
};
After the user grant permission check the login_url, you may access user id.
Try this
Have you checked you have the correct URL in the configuration of your app in Facebook?
I have this piece of code to my FB apps and work properly, could you try it?
$session = $this->facebook->getUser();
if ($session) {
$logoutUrl = $this->facebook->getLogoutUrl();
try {
$data['uid'] = $this->facebook->getUser();
$me = $this->facebook->api($session);
$data['me'] = $me;
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $this->facebook->getLoginUrl(
array(
'scope' => 'publish_stream, user_about_me, email',
'redirect_uri' => 'YOUR URL',
'canvas' => 1,
'fbconnect' => 0,
'display' => 'page',
));
}
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$this->load->view('MY VIEW', $data);

Asking for permission using new PHP SDK (3.X.X)

How can I ask for permissions using new PHP SDK? I don't want to use the graph api and parse the url all the time. When the application is opened it should automatically ask for permissions if the user hasn't granted one already.
Here's how i'm doing it with the latest PHP SDK (3.0.1)
// init new facebook class instance with app info (taken from the DB)
$facebook = new Facebook(array(
'appId' => 'YOUR APP ID',
'secret' => 'YOUR APP SECRET'
));
// get user UID
$fb_user_id = $facebook->getUser();
// get the url where to redirect the user
$location = "". $facebook->getLoginUrl(array('scope' => 'publish_stream, email'));
// check if we have valid user
if ($fb_user_id) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$fb_user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$fb_user_id = NULL;
// seems we don't have enough permissions
// we use javascript to redirect user instead of header() due to Facebook bug
print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';
// kill the code so nothing else will happen before user gives us permissions
die();
}
} else {
// seems our user hasn't logged in, redirect him to a FB login page
print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';
// kill the code so nothing else will happen before user gives us permissions
die();
}
// at this point we have an logged in user who has given permissions to our APP
// basic user info can be fetched easily
print "Welcome to my app". $fb_user_profile['name'];
Session Based Login with scope and Logout with access_token for PHP-SDK 3.2.0.
<?php
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => '135669679827333',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxx',
));
$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;
}
}
if ($user) {
$params = array(access_token => ''.$access_token.'');
$logoutUrl = $facebook->getLogoutUrl($params);
} else {
$params = array(
scope => 'read_stream,publish_stream,publish_actions,read_friendlists',
//redirect_uri => $url
);
$loginUrl = $facebook->getLoginUrl($params);
};
$access_token = $_SESSION['fb_135669679827333_access_token'];
?>
.
<?php if($_SESSION['fb_135669679827333_access_token']): ?>
Login & Connect
<?php else: ?>
Login & Connect
<?php endif ?>

Categories