How to get user profile id in facebook php sdk - php

https://www.facebook.com/profile.php?id=100010558444183
I want to get this profile id when user login with Facebook.
I tried with this but it gives something else.
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me',$access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
$id = $user['id']; ```

You cannot get the "real" id anymore, only an App Scoped ID that is unique and can still be used for identifying returning users. If you want to get a link to the user profile, use the link field: https://developers.facebook.com/docs/graph-api/reference/v3.2/user
For example:
$response = $fb->get('/me?fields=name,link', $access_token);
More details: https://github.com/facebook/php-graph-sdk/blob/5.x/docs/examples/retrieve_user_profile.md

Related

How to get app_domains attribute array from Facebook API

Following at the Facebook API docs I'm able to return only 4 attributes from my FB app.
My Code:
$user = Auth::user();
try {
$response = $this->facebook->get('/'.$user->facebook_app_id, $user->facebook_access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
dd($graphNode);
This only returns 4 attributes
I need it to also return app_domains. Looking at the docs it looks like it should? Is there a way to return the app_domains array from a FB app? FB app API
Looks like you'll need to use something like this:
$response = $this->facebook->get('/'.$user->facebook_app_id,
['fields' => 'app_domains'],
$user->facebook_access_token
);

FB Post in the Feed of a Group

I have the following PHP code to post in a Facebook Group Feed where the the publisher is admin of. The code is as follows:
$post_array=array ('link'=>$link_share,'message' => $message);
try {
$response = $fb->post(
"/{$group_id}" . "/feed",
$post_array,
"{$access_token}"
);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
But when I execute it I get the error:
Graph returned an error: Unsupported post request. Object with ID 'group_id' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api
I read all the documentation, the user has the permissions to publish in the group but I keep getting that error. Any ideas why?
Thanks

Facing issue to Show Profile Picture of facebook using PHP sdk v.5

My code
try {
$response = $fb->get('/me?fields=id,name,gender,email,picture', $accessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name:'.$user['name']."</br>";
echo 'gender:'.$user['gender']."</br>";
echo 'picture:'.$user['picture']."</br>";
echo 'email:'.$user['email']."</br>";
My output
Name:Siddhu Siddhartha Roy gender:male
picture:{"is_silhouette":false,"url":"https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/12295307_890342681035161_8265283125930543183_n.jpg?
oh=f8198fc14435078e584749f2d55dcd8e&oe=5833FDCD"}
email:siddhu.08pc1a0413#gmail.com
My Problem
I'm unable to show profile picture.. when I goto URL
https://scontent.xx.fbcdn.net/v/t1.0-1/p50x50/12295307_890342681035161_8265283125930543183_n.jpg
it shows that Content not found
Simple: You're getting the data in JSON format. So decode the result.
$json = json_decode($user['picture'], true);
echo $json['url'];

PHP Facebook SDK.About the access Token

Now I can get my own information such as my id,username.But when I use this access Token in order to get my friend lists,then the error is :
An active access token must be used to query information about the current user.
What is this mean?why the access Token doesn't work?
the first php:
$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('http://my_ip/facebook/login-callback.php', $permissions);
the second php:
(I can get my information):
try {
$response = $fb->get('/me');
var_dump($response);
echo "<br>";
$userNode = $response->getGraphUser();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
//echo "<br>";
echo 'logged in as the id '.$userNode->getId()."<br>";
echo 'Logged in as ' . $userNode->getName()."<br>";
echo 'the user picture is '.$userNode->getPicture()."<br>";
$picture = $userNode->getPicture();
and:
$permissions = ['user_friends'];
$friendsUrl = $helper->getLoginUrl('http://my_ip/facebook/friends-callback.php',$permissions);
finally,in the third php,I use the same way to get the information,but it is failed.
I think you missunderstood the concept of Facebook API.
First you have to get user permission to accept your request:
$permissions = ['email', 'user_friends']; // Permissions
$loginUrl = $helper->getLoginUrl('http://my_ip/facebook/login-callback.php', $permissions);
Then, in your login-callback.php file you can access the API according to requested permissions. For friends list:
$response = $fb->get('/me/friends');
var_dump($response);
You don't have to login every time after yout first request. The access token will be saved in session.

Facebook user Graph does not return Email PHP

I am new to the whole Facebook API.
I am creating a login page for users, and the idea is that they login with Facebook.
At the top of login page I have the following code:
$fb = new Facebook\Facebook([
'app_id' => "$fb_appid",
'app_secret' => "$fb_appsecret",
'default_graph_version' => 'v2.2',
]);
Then a little lower I have the login button:
if(isset($fb)){
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // optional
$fb_login_url = $helper->getLoginUrl("$server_url/public/facebook/login-callback", $permissions);
} else $fb_login_url = "";
if(strlen($fb_login_url) >= 1) echo "<a href='$fb_login_url'><i class='fa fa-2x fa-facebook-square'></i></a>";
else echo "<a href='#' disabled='disabled'><i class='fa fa-2x fa-facebook-square'></i></a>";
The code works up until there, if I click the button, I go through to the Facebook portal, click the allow for the permissions and whatever, then it redirects me to login-callback.
The code on login-callback is:
if(isset($fb)){
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
$err_message = 'Graph returned an error: ' . $e->getMessage();
access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
} catch(Facebook\Exceptions\FacebookSDKException $e) {
$err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
}
if (strlen($err_message) <= 0 && isset($accessToken)) {
$_SESSION['facebook_access_token'] = (string) $accessToken;
$fb->setDefaultAccessToken("$accessToken");
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
// $username = $userNode->getName();
// $firstname = $userNode->getFirstName();
// $lastname = $userNode->getLastName();
print_r($userNode);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
$err_message = 'Graph returned an error: ' . $e->getMessage();
access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
} catch(Facebook\Exceptions\FacebookSDKException $e) {
$err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
access_log("facebook_login","IP:$client_ip\r\nFacebook Error: $err_message");
}
}
}
The $userNode looks like this:
Facebook\GraphNodes\GraphUser Object
(
[items:protected] => Array
(
[name] => Jacques Koekemoer
[id] => xxxxxxxxxxxxxxxxxx
)
)
I have set the permissions to allow for the email, and if I am not mistaken the public profile is sent automatically.
I have also checked that the button on the login page does have "&scope=email". Below is the code that I have right on the page right now in the login button:
https://www.facebook.com/v2.2/dialog/oauth?client_id=xxxxxxxx&state=xxxxxxxx&response_type=code&sdk=php-sdk-5.0.0&redirect_uri=http%3A%2F%2Fxxxxxxx.xxxxxxxxxxxx.co.za%2Fpublic%2Ffacebook%2Flogin-callback&scope=email
I replaced the client_id, state and domain name because I don't want that information available publicly as I don't know what people can do with it.
Let me know if it is needed to solve the problem.
I used the Facebook guide here to setup and download the SDK.
I solved the problem.
You need to request specific fields from Facebook in the get function.
$response = $fb->get('/me?fields=id,name,email');
This can be found here.
A full list of fields that you can query can be found here.
Example
if (isset($fb)) {
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
$err_message = 'Graph returned an error: ' . $e->getMessage();
access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
} catch (Facebook\Exceptions\FacebookSDKException $e) {
$err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
}
if (strlen($err_message) <= 0 && isset($accessToken)) {
$_SESSION['facebook_access_token'] = (string)$accessToken;
$fb->setDefaultAccessToken("$accessToken");
try {
// this here is where you specify the fields
$response = $fb->get('/me?fields=id,name,email');
$userNode = $response->getGraphUser();
/* handle the result */
} catch (Facebook\Exceptions\FacebookResponseException $e) {
$err_message = 'Graph returned an error: ' . $e->getMessage();
access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
} catch (Facebook\Exceptions\FacebookSDKException $e) {
$err_message = 'Facebook SDK returned an error: ' . $e->getMessage();
access_log("facebook_login", "IP:$client_ip\r\nFacebook Error: $err_message");
}
}
}
One of the my friend was trying to the issue from last 5 days. huummm very irritating. which got love in 2 min...
Assumption: using PHP for O2Authentication
Solution:
$loginURL = $fb->getLoginUrl({your redirect URL},['email']);
And job is done.
Thanks & Regards
Jaiswar Vipin Kumar R.
Check you app version if it is version 2.2 or lower, it will work fine , it is not than it will not return. for that you have to request api with scope for email access.

Categories