Using the PHP SDK. I can log in and the app goes through the authentication ok (despite FB's greatest efforts to stop me!). The email displays courtesy of <?php print_r($user_profile); ?>
However, the array at the top of the example page always shows
[id] => 2
[oauth_uid] => 5
[oauth_provider] => facebook
[username] => 5
When I try to output the email, it again shows a value of 5. How do I grab the email address? Why is it displaying 5?
You need to do something like:
<?php
$user_profile = $facebook->api('/me');
print_r( $user_profile );
If you are asking for the email permission, the full email address will be visible.
Related
I really need a solution. Facebook messages seem to be "read-only".
I use javascript.
f.e. {conversation-id}/messages?message={"hello there"}
(not working)
You can use their chat API until April 2015. After that, its gone.
No, you can't send private message via Facebook GRAPH API, you can't do that via FQL too, but there is a solution:
There is a way to send message to user's primary inbox, you need a library which is completely open source (by me) then:
<?php
require_once('send.message.php'); //requires file
require_once('facebook.php'); //sdk provided by facebook, you need one for tokens and
//all
$config = array('appId' => '1496661733888719' ,'secret'=>'0c959ab2dec71c5a53aab1cd64751223' );
$facebook=new Facebook($config);
if(!$facebook->getUser()){
$params = array(
'scope' => 'read_mailbox, xmpp_login',
'redirect_uri' => 'http://localhost/'
);
$url=$facebook->getLoginUrl($params);
header("location:".$url);
die();
}
$messageobj=new SendMessage($facebook);
$receiverId=''; // this may either be username or userID, this class takes care of both the //cases
$body='test message';
if($messageobj->sendMessage($body,$receiverId))
{
echo 'message sent';
}else
{
echo 'some error occured';
}
?>
In this way you can send message to users, you can get the detailed information on this at:
nishgtm.com/2013/11/facebook-message-api-php/
You will also find the link to github page on the above link. This library is super easy to use. Feel free to ask me anything via comments.
I know it might be duplicate question but please help. I have created a app and installed it on my Facebook page. My issue when I authenticate it using authentication method I got the user id... But when I go through proper signed request method (i.e. When I login with facebook site only.) I don't get user. how can I get userid in case of signed request that When I login using facebook site.
My signed request.
$facebook = new Facebook(array(
'appId' => '****',
'secret' => '****',
'cookie' => true,
));
echo 'User ID :- ' . $user = $facebook->getUser();
$signed_request = $facebook->getSignedRequest();
echo '<pre>';
print_r($signed_request);
echo '</pre>';
User ID :- 0
Array (
[algorithm] => HMAC-SHA256
[issued_at] => 1372676199
[page] => Array
(
[id] => 544896778905441
[liked] =>
[admin] => 1
)
[user] => Array
(
[country] => in
[locale] => en_US
[age] => Array
(
[min] => 21
)
)
)
How can I get User id.
** EDIT **
As of a few months ago (Dec 2014) Facebook no longer allows Like-Gating content. I guess that's why someone is going around placing negative votes and links to their own answers. So the below is no longer valid
There is a difference between getting a signed_request for a user who has authorised an app, which is your first case and getting the user id from a signed request on a page app. In the second example you can test if a user likes your page, but you can't get the specific user id. They are just interacting with your page - yes they may be logged into Facebook, but they haven't explicitly authorised your app. I presume this is what you mean by "login with Facebook site only"
If you look at https://developers.facebook.com/docs/reference/login/signed-request/ you will see it states
Some fields and values, the user_id and oauth_token for example will only be passed if the user has logged into your app.
They may be logged into Facebook, but not have logged in/authorised your app
You can follow the instruction on how to decode the signed request here
https://developers.facebook.com/docs/facebook-login/using-login-with-games#parsingsr
OR
You can use the service that I created. It will return decoded signed request :)
https://websta.me/fbappservice/parseSignedRequest/<concat signed request here>
//angularjs
you may get it via $http.jsonp(url);
//jquery // refer to jquery docs on getting jsonp requests
$.getJSON( url, {
format: "json"
})
In my facebook App, I am submitting the form to the same page.
After submitted, I will check the user is logged in by getUser(). If a user id can be got, POST data will be stored to database.
The below code works in Chrome, Firefox, Safari in Both Mac and PC.
However, when I first submit the form in IE 8, getUser() will return 0. Then I submit the form again, the user ID can be got.
I have not idea why the user ID cannot be got at the first submission. Can anyone help?
Thanks!
<?php
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET_KEY,
));
// Get User ID
$user = $facebook->getUser();
?>
<form action="" method="post" id="form1" name="form1">
...
</form>
I don't see the rest of code, but if you ask is you has $user, or not you have to request autentification or login. there is something like this:
if(!$user) make the login request
else continuos with your code.
sees the sdk php code or javascript library for Facebook in the Facebook developer side.
other problem is possible that you have and error while you try to get the user, and its possible you have to do a
try{
$user = $facebook->getUser();
}catch (exception *e){
//is there is and error you have to login again to get the users data
}
I hoper this will be useful for you
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is Facebook PHP SDK getUser always returning 0?
Am using the following code to authenticate Facebook uses and redirecting them to login page if not logged in or require permission. Problem is getUser() always returns 0 causing the code to get stuck in a redirecting loop. Any ideas?
include_once("facebook.php");
$app_id = 'xxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// initialize facebook
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
$user = $facebook->getUser();
echo $user;
if ($user) {
try {
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
else
{
$loginUrl = $facebook->getLoginUrl(array('scope' => 'email'));
header('Location: '.$loginUrl);
}
After days of searching I found that I just wasn't adding a 'code' key value when using the Facebook API. The system needs it in order to validate
When you use the API function $facebook->getLoginUrl(); it takes you to a login-page that (when you authenticate properly) simply returns you to your own website with a code="x" parameter in the navigation bar. So when instantiating the Facebook Object you simply use a get request to nab this piece of information.
when your page reloads the Facebook API, make sure that one of its key values is
'code' => $_GET['code'],
sorted..
Really angry that I had to figure this out myself.. Look at the getLoginUrl() docs here http://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/ and you'll find that nowhere does it specify what it returns, just that it "authorises" the app, whatever the hell that means.
Also on the main page of the PHP API docs found here http://developers.facebook.com/docs/reference/php/, it states that the minimum number of parameters required are an app secret and an appID. If this is the case, then why does adding the code sort my problem?
If I'm talking nonsense, please respond. I like to know the error of my ways :).
This is not a sdk related problem, SDK (3.1) working well. getUser() and PHP-SDK silently fails if _REQUEST like globals dropping by http server if misconfigured. I was using wrong-configured nginx and after tracing code ~3 hours solved this problem via vhost configuration change.
How can I hide this "?state=...&code=..." in the address after logging in using php sdk 3.1.1
Check for
if(isset($_GET['code'])){
$user = $facebook->getUser();
$access_token = $facebook->getAccessToken();
}
Facebook returns userID only after the login and that code.
You can name a folder "facebook" and put the auth code and probably a session saver or cookie.
Then you can use the session to store the info and proceed on the other sites without getting the state and code parameters in the url.
I think that will work because a norwegian site use this, and it works pretty great.
Good luck :)
Molty
Facebook will send you code every time user sign up you app or login to your app. So what i do is redirect/reload again
if (isset($_Get['code']) and $_Get['code'] !='' ){
$uid = $facebook->getUser();
echo "<script type='text/javascript'>top.location.href = '".APP_URL."';</script>";
exit;
}
Here APP_URL is something like apps.facebook.com/greatapp/ or you can use Site URL which you set in developer.