Facebook Graph API (How to get user gender?) - php

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

Related

How to get email from fb library [duplicate]

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".

New PHP facebook SDK getRequestForNextPage

I have no idea why the getRequestForNextPage(); doesnt work.
I use it like this:
$photoRequest = new FacebookRequest( $session, 'GET', '/'.$albumId.'?fields=photos.limit(30){picture,created_time,id,updated_time,width,height,from,link}' );
do{
pretty_print($photoRequest);
$response = $photoRequest->execute();
$graphObject = $response->getGraphObject()->asArray();
foreach($graphObject['photos']->data as $index => $photo){
// work on photos
}
}while ($photoRequest = $response->getRequestForNextPage());
I know for certain theres more then 30 pictures on 2 of the albums i go through, yet it still shows only the first 30.
When I var_dump $photoRequest = $response->getRequestForNextPage() it always returns null..
Is this the way to use this method?

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}" />';
}

Getting the users who retweeted a tweet

I'm using TwitterOAuth lib to query for tweets and for people who retweeted the tweets.
The first query (get the tweets itself) works like a charm, but the second query (get the people who retweeted each tweet) is always returning Sorry, that page does not exist, error 34.
This is how I'm calling the lib:
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
return $twitter->get('statuses/retweets/' . $id . '.json', $query);
Why is Twitter's API returning this error?
TwitterOAuth adds the '.json' bit on for you. Try:
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
return $twitter->get('statuses/retweets/' . $id, $query);

$me = $facebook->api('/me'); returns empty string

This piece of code used to work as expected, but now it seems i can't retrieve the user name the same way:
$uid = $facebook->getUser(); // returns the facebook id of the user
$me = $facebook->api('/me'); // returns empty. why?
Due some facebook api updates i had to change the getSession from:
$session = $facebook->getUser();
to
$session = $facebook->getUser()>0;
So how to retrieve the user name in facebook?
So how to retrieve the user name in facebook?
What about this :
$pageContent = file_get_contents('http://graph.facebook.com/USERID');
$parsedJson = json_decode($pageContent);
echo $parsedJson->name;
Facebook use CURL for send request to server, maybe curl on your server not work or curl_exec is disabled

Categories