I want get my facebook page details. I am using php sdk 4.0 and facebook documentation gives
/* PHP SDK v4.0.0 */
/* make the API call */
$request = new FacebookRequest(
$session,
'GET',
'/me/accounts'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
but After running this code
My output is
Facebook\GraphObject Object ( [backingData:protected] => Array ( ) )
so If i miss anything in my code Or any other things
Thanks
If you use a test app (the main app was not submitted for fb review) you should use the test account to make request. On real account will give you an empty object/array.
More info
https://developers.facebook.com/docs/apps
Read about test apps and test users.
I got the solution using facebook extended profile management
http://developers.facebook.com/docs/authentication/permissions
You can do this if you are using Facebook, connect by passing scope=email in the get string of your call then to the Auth Dialog.
It is in the Facebook doccumentation
Related
I need to create an admin panel of sorts for a client that will present him with data of conversion values over specific time intervals, for his eshop which has the New Facebook pixel installed.
Assume I've properly configured the Standard Event I want to track. The part where I'm stuck is actually reading the data via the Facebook Marketing php sdk. It is supposed to look something like that:
/* PHP SDK v5.0.0 */
/* make the API call */
$request = new FacebookRequest(
$session,
'GET',
'/{ads-pixel-id}/stats'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
Am I looking at the right place? Can I still get these kind of stats or is this behavior deprecated? The Facebook documentation isn't very clear on this and everywhere I look I see deprecated pages and code,
You need to add an aggregation like this:
/{ads-pixel-id}/stats?aggregation=event
I am integrating Facebook login in my site. I am using Facebook SDK 4.0. My app API version is 2.4. I am trying to get basic profile info when user successfully log in with Facebook. But problem is - I am able to get only user id and his full name. Other stuffs like first name, last name, gender etc. are not being received. Also of course I need email of the person and user profile picture later on some stage. Here is my working code which is printing only name and id:
<?php
// start session
session_start();
$app_id = 'app_id'; // Facebook App ID
$api_secret = 'app_secret'; // Facebook App Secret
$required_scope = 'public_profile, publish_actions, email'; // Permissions required
$redirect_url = 'site_url/index.php'; // FB redirects to this page with a code
require_once __DIR__ . "/facebook-php-sdk-v4.0/autoload.php";
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($app_id,$api_secret);
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( $redirect_url );
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();
// print data
echo "<pre>";
echo print_r( $graphObject, 1 );
echo "</pre>";
} else {
// show login url
echo 'Login';
}
Following info is printed:
Facebook\GraphObject Object
(
[backingData:protected] => Array
(
[name] => Fname Lname
[id] => ID
)
)
You can see that I have already given scope parameter. But its not helping me. Then I started search on STO and found some solution which suggest us to add following line of code if you are using API v2.4 to explicitly mention the fields you want:
$request = (new FacebookRequest($this->session, 'GET', '/me/?fields=friends,id,name,birthday,email,picture,gender,location,address,email,hometown'))->execute()->getGraphObject()->asArray();
But when I used above code, it again didn't work instead i got blank screen not even id and name as before.
I also visited Facebook developer docs but they are so long, complex and not enough user friendly a developer can follow them easily in short time.
I think there is a lot of confusion regarding Facebook login. There are so many articles on web and each illustrating their own method. Some uses PHP and other uses JavaScript too which popups the login window. Honestly speaking I am very confused at this level and can't understand which is the best, recommended and working method of accomplish the task. Thus I'm in search of following questions:
I want help in making my above code working so that it can fetch all info about user including email and profile pic too.
Some method of login with Facebook takes user full login page and some other displays a popup for login and user remains on the same page after successful login which initially generated the popup window. How both are different?
What is best practice of login with Facebook. Also which SDK and Graph API version one should use?
While Generating a login URL you must pass on the scopes for which you seek permission. You can generate this by modifying,
echo 'Login';
to
echo 'Login';
This is just a example and may not cover all your requirement. You may have to add more scopes as per your requirement.
Above three scopes are permitted by default and you don't have to go through an app verification by facebook. But if you add any other scope, you may have to go through a an approval process before you can go live with an app.
And of-course since v2.4 you must explicitly ask for each field. Before email was supplied by default with /me, not any more. you must add it like,
/me?fields=id,first_name,last_name,friends,email
Login with a facebook will not ask for a login if user is already logged in and will go to permission screen straight away if user has not given permission to this app before. If user has already given all permission and he is already logged in facebook, clicking on login with facebook link will redirect user to redirect handler straight away.
I suggest, you should always use latest stable facebook apis.
UPDATE
Just realized that facebook allows account creation with a phone-number. So, if user has created an account with phone-number instead of an email, in api response you will receive phone-number in place of email like below,
...
'email' => '465454654654'
...
So, your code must handle this situation.
I'm trying to implement Facebook payments (on canvas), what I've got so far, is that FB is calling my callback URL, I can get the payment's ID, and now I'm supposed to call Graph Api to get the details. And this is what I can't do.
Here ( https://developers.facebook.com/docs/graph-api/reference/v2.2/payment ) is an example of doing it in PHP:
$request = new FacebookRequest(
$session,
'GET',
'/{payment-id}'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
It returns NULL. They say later that:
An app access token for the app that created the payment is required.
I have no idea how to get one and then how to send it along with the above request.
Can somebody help?
You need to make the following get request:
https://graph.facebook.com/oauth/access_token?client_id={app_id}&client_secret={app_secret}&grant_type=client_credentials
I am using the following to make my requests to Facebook
$facebook->api('/feed',
array(
'access_token' => $_SESSION['fb_access_token'],
));
However, I want to get the news feed of the user.
Not to be confused: I do not want the feed of a certain user, much less of a given page.
I want the News Feed/Feed Stream the user views when accessing Facebook, one that contains the posts of your friends, etc.
How is this possible?
Make an API call to /me/home or /user_id/home where user_id is the current session user.
You are also using the old PHP SDK
/* PHP SDK v4.0.0 */
/* make the API call */
$request = new FacebookRequest(
$session,
'GET',
'/me/home'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
Reference: https://developers.facebook.com/docs/graph-api/reference/v2.2/user/home
UPDATE:
The latest Graph API doc suggests the following:
As of October 6th, 2015, this endpoint is no longer available. Please consider using the /user-id/feed edge instead.
This effectively means we cannot access the user news feed provided by Facebook but will have to rely on /user-id/feed as an alternative.
The endpoint is me/home.
https://developers.facebook.com/docs/graph-api/reference/v2.2/user/home
The posts that a person sees in their Facebook News Feed.
Getting all recent notifications including read via Graph API, call:
graph.facebook.com/USER_ID/notifications?include_read=true
So I have a friend who is a musician and has a page set up. He has asked me if there is a way of getting all the events on his Facebook Page via the Graph API and then putting them onto his website. I said I'd look into if this is even possible given that the API is now at version 2 and the PHP SDK has been bumped up to version 4.
Does anyone know how I'd do this? I already know how to make 'GET' request using the new PHP SDK, however I don't seem to be able to get the necessary fields.
Any help would be great!
Thanks!
It's given on developers.facebook.com
/* make the API call */
$request = new FacebookRequest(
$session,
'GET',
'/{page-id}/events'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
More at https://developers.facebook.com/docs/graph-api/reference/v2.0/page/events
Hope this Helps.