How to get email from fb library [duplicate] - php

This question already has answers here:
Facebook JS SDK's FB.api('/me') method doesn't return the fields I expect in Graph API v2.4+
(4 answers)
Closed 6 years ago.
How to get email and contact from facebook api. $graphObject gives only
name and id. how to solve this?
if( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
echo "<pre>";print_r($graphObject); exit;
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FBFULLNAME'] = $fbfullname;
$_SESSION['FBEMAIL'] = $femail;
/* ---- header location after session ----*/
header("Location: index.php?act=fbmobile");
}

This would be the correct API call:
/me?fields=name,email
Since v2.4 of the Graph API, you have to specify the fields. It is called "Declarative Fields".

Related

php facebook sdk issue multiple database

I'm using php facebook sdk on my website I have two subfolders each has a different database but after facebook auth it redirects to the main url of the site and does not save the data to the correct database. The data should be submitted to header("Location: https://123.com/uae/user/fblogin"); but insteaded it is redirected to header("Location: https://123.com/user/fblogin");
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me?locale=en_US&fields=id,name,email,first_name,last_name' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name');
$femail = $graphObject->getProperty('email'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
$first_name = $graphObject->getProperty('first_name'); // To Get Facebook email ID
$last_name = $graphObject->getProperty('last_name'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FB']['fbid'] = $fbid;
$_SESSION['FB']['fullname'] = $fbfullname;
$_SESSION['FB']['email'] = $femail;
$_SESSION['FB']['first_name'] = $first_name;
$_SESSION['FB']['last_name'] = $last_name;
//echo '<pre>'; print_r($_SESSION);die;
if(isset($_SESSION['connect']) && $_SESSION['connect'] == 1){
unset($_SESSION['connect']);
header("Location: https://123.com/uae/fbconnect/");
}else{
header("Location: https://123.com/uae/user/fblogin");
exit;
}

Unable to login through facebook php sdk

While login wiht fb php sdk, I am getting such kind of error
This page isn’t working
www.facebook.com is currently unable to handle this request.
HTTP ERROR 500
I had configure my app and my secret key properly according to facebook app and key.
I am not getting why such kind of error is thrown. Please Help me
// init app with app id and secret
FacebookSession::setDefaultApplication(
myappkey,mysecretkey );
// login helper with redirect_uri
$helper = new
FacebookRedirectLoginHelper('http://demos.krizna.com/1353/fbconfig.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
/* ---- header location after session ----*/
header("Location: index.php");
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}

PHP Facebook API - Email return null

I have code like this:
$helper = new FacebookRedirectLoginHelper('http://MyAwesomeWebsite.com/' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
echo print_r($ex);
} catch( Exception $ex ) {
echo print_r($ex);
}
if ( isset( $session ) ) {
$request = new FacebookRequest( $session, 'GET', '/me?locale=en_US&fields=name,email' );
$response = $request->execute();
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
}
But every time $femail is null. Why? What I'm doing wrong?
When you log in the user, you have to pass along the email permission in the scope parameter.
So when you call your helper, also initialise a $permissions array:
$helper = new FacebookRedirectLoginHelper('http://MyAwesomeWebsite.com/' );
$permissions = ['email', 'public_profile'];
There should be $helper->getLoginUrl call in somewhere in your code. In that call you should pass the $permissions variable as the second parameter:
$loginUrl = $helper->getLoginUrl($placeYourCallbackURLHere, $permissions);
That asks the user explicitly for a permission to get the user's email. Note that the email can also be empty, if the user has:
Registered to Facebook via phone number (does not have email address in Faceebook)
Selected that they do not want to provide the email address at the login prompt.

Facebook Graph API (How to get user gender?)

I'm working on PHP project. I have to get id, fullname, firstname, gender and email of the user. I got id of the user. So, It is clear that there is no any problem in PHP facebook-sdk or Facebook library. But I can not get other fields. So, ther is something is wrong in my code. But I can't find that wrong code. If anyone know answer then please explain or suggest me link from where I can understand from beginning. Thank You. Here is my code. Here is my code.
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
echo $fbid; //working as I'm getting facebook ID
$filenameIn = 'https://graph.facebook.com/' . $fbid . '/fields=gender';
$gender = file_get_contents($filenameIn);
echo $gender; //I can not get this.
CBroe is right. I have made that mistake. As I changed my code as CBroe has suggested, I got problem Solved. Here is correct code.
$request = new FacebookRequest($session, 'GET', '/me?fields=id,gender');
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbgender = $graphObject->getProperty('gender'); // To Get Facebook gender
echo $fbid; //working as I'm getting facebook ID
echo $fbgender;//working as I'm getting facebook gender

how can i fetch all my photos using FB API for php

I am new in Facebook APPS and i want to get all photos that the user has in his account after login
i made the login and the permission well
$request = new FacebookRequest( $session, 'GET', '/me/photos' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
when i print the array using print_r( $graphObject, 1 ),it gives me long array i got confused
i want fetch the photos only to be able to put it into loop
think like this echo $graphObject['data']['0']['source'];
Once you get the result from the API call, you can output the data to your page as:
foreach ( $graphObject['data'] as $photo ) {
echo '<img src="{$photo->images[0]->source}" />';
}

Categories