This question already has answers here:
Facebook getUser() returns 0 [duplicate]
(5 answers)
Closed 9 years ago.
I am using facebook php sdk in zend. i am not getting user id even i have logged in facebook in another tab. Please help me to fix this issue.
$facebook = new \Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true,
));
$user = $facebook->getUser();
echo $user;
when i am using above code $user return 0 some times.
remove the \ before facebook. Correct way will be:
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true,
));
Let me know if this helped.
kind regards.
Related
i want to send notification who access my app.i use following code.
but it not work.i was goggling about it.but every example looks like my one.but it not work for me.
here is my code
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'myappid',
'secret' => 'myappsecret',
));
$app_id='myappid';
$app_secret='myappsecret';
$app_access_token=$app_id.'|'.$app_secret;
$user_id='359211054272153';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$response = $facebook->api( '/359211054272153/notifications', 'POST/v2.2/', array(
'template' => 'You have received a new message.',
'href' => 'RELATIVE URL',
'access_token' => $app_access_token
) );
i am used old version of Facebook sdk it want to real user id.
but currently
Facebook gives app scoped userid. thats the error.
using new sdk v4 it not problem.
We are trying to get user information from getUser() method of facebook using php sdk (3.1.1) but it returns 0. what would be the cause?
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
$facebook->getUser();
The latest SDK is v.3.2.2 https://github.com/facebook/facebook-php-sdk/ you should use that instead and also be using a login handling logic with getLoginUrl()
how can i get the current logged in userid ?
im pretty sure my appId is correct
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
));
im using this method but i returns 0;
$facebook->getUser();
If you want to get facebook user id with this code
$facebook = new Facebook(array(
'appId' => 'your_app_id',
'secret' => 'your_app_secret',
));
$user = $facebook->getUser();
You have condition to should have done before, the first condition is you put that code in the page that redirected after the user login via fb provide by this code
$params = array(
'display'=>'popup',
'redirect_uri' => 'http://example.com'
);
$loginUrl = $facebook->getLoginUrl($params);
OR the second condition is you already have the user 'Access Token', so the code will be like this
$facebook = new Facebook(array(
'appId' => 'your_app_id',
'secret' => 'your_app_secret',
));
$facebook->setAccessToken('user_access_token');
$user = $facebook->getUser();
If you don't clear about the Facebook Login flow using PHP SDK, you can visit this page
$myData=$facebook->api('/me');
$myid=myData['id'];
$myName=myData['name'];
$myUsername=myData['username'];
[id] => your id
[name] => your full name
[first_name] => your firs name
[last_name] => lastname
[link] => profil link
[username] => username
[gender] => your gender (male,female)
[email] => your mail (if have email permission)
[timezone] => 2
[locale] => your locale
[verified] => 1
Follow this guide: Login for Server-side Apps. Basically, you have to generate a login URL, receive a code generated by Facebook, and with this code, request for an access token.
I have this code in php that used to work, but now it doesn't, and even thoug it sounds like a lie, I did not touch anything!
$facebook = new Facebook(array(
'appId' => 'myApp',
'secret' => 'mySecret',
'cookie' => true ,
'scope' => 'read_stream','manage_pages'
));
$user = $facebook->getUser();
if ($user!=0) {
doStaff;
}else{
$login_url = $facebook->getLoginUrl($params = array('scope' => "read_stream"));
echo ("<script> top.location.href='".$login_url."'</script>");
}
The problem now is that gets stack in an infinite loop.
Any help would be really appreciate.
You are probably using the deprecated Facebook PHP SDK.
Try to get the latest Facebook PHP SDK
from facebook docs it says:
"You can post a score or a user by issuing an HTTP POST request to /USER_ID/scores with the app access_token as long as you have the publish_actions permission."
well i have publish_actions permission and i'm trying the following code but it doesn't work...
require('php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => '123546321654654654654',
'secret' => 'd9132156afasdfadsfsdfewfg79f5f16d0',
'cookie' => true
));
$attachment = array('score' => 1005);
$postscore = $facebook->api('/12345612/score/','POST',$attachment);
You need to send your app access token in the attachment array. So simply update your array attachment to
$attachment = array( 'score' => 1005, 'access_token' => $facebook->getAppId().'|'.$facebook->getApiSecret() )
$postscore = $facebook->api('/12345612/score/','POST',$attachment);
I'm not familiar with php but link for posting the score is "/USER_ID/scores/". You have forgotten one 's' in the end of the scores.