i'm trying to get the facebook user name using the API
this is my code:
$facebook = new Facebook(array(
'appId' => $this->app_id,
'secret' => $this->app_secret,
'cookie' => true,
));
$session = $facebook->getUser();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
echo $uid;
exit();
$me = $facebook->api('/me');
echo $me;
exit();
} catch (FacebookApiException $e) {
error_log($e);
}
$personLastName= $me['last_name'];
var_dump($personLastName);
exit();
the line where it crashes:
$me = $facebook->api('/me');
No errors displayed, just white page and no log error in apache
Some help would be welcome!
Thanks in advance
You are try to print the first element and then you have terminate the statement with exit; then the rest of the code will not be executed.Try this
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
echo $uid;
$me = $facebook->api('/me');
echo $me;
} catch (FacebookApiException $e) {
error_log($e);
}
$personLastName= $me['last_name'];
var_dump($personLastName);
exit();
}
Related
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
function fblogin(){
$base_url=$this->config->item('base_url');
$facebook = new Facebook(array(
'appId' => $this->config->item('appID'),
'secret'=> $this->config->item('appSecret'),
));
$user = $facebook->getUser();
if($user){
try{
$user_profile = $facebook->api('/me');
$params = array('next' => $base_url.'Poll/logout');
$ses_user=array(
'User' => $user_profile,
'logout' => $facebook->getLogoutUrl($params)
);
$this->session->set_userdata($ses_user);
header('Location: '.$base_url);
}catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
}
echo error_log($e);
$user = $this->session->userdata('User');
echo $user['id'];
$this->load->view('main');
}
when echo error_log($e) shows (1)
and var_dump(error_log($e)) shows bool(true)
but in localhost working fine
worked in php sdk without js
but still dont know reason
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;
}
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);
i am using basic facebook php sdk to login users to my page via facebook.
facebook.php, base_facebook.php and:
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if (!$user) {
header("Location: /");
} else {
$loginUrl = $facebook->getLogoutUrl();
and i want to allow my users to post (or skip) something to their wall's when logging in. like that:
http://developers.facebook.com/attachment/web_dialog.png
any ideas?
to post to a user's wall you'd do something like this:
if (isset($_POST['status'])){
try {
$post = stripslashes($_POST['status']);
$statusUpdate = $facebook->api('/<fb_user_id>/feed?access_token='.$session['access_token'], 'post', array('message'=> strip_tags($post), 'cb' => ''));
} catch (FacebookApiException $e) {
echo "<pre>";
echo $e;
echo "</p>";
}
}
You can style the form that posts to that script to match that in the png file.
hope that is what you are looking for and helps you.