How we get conversations messages from Facebook Using PHP Sdk v5.0 - php

start from beginning,i fetched Facebook Page id,access token,user access token,user id user emails,means i have lot of information about a facebook user profile and pages details,now i want to fetch conversations messages between user and page,i have all privilege to read message and i get messages through graph api explorer but its quite different to implement with programmatically.
so if anyone have knowledge how to get messages with php sdk code then please update me. thanks in advanced.

$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.4'
]);
$fb->setDefaultAccessToken({access-token});
$responsePosts = $fb->get('me?fields=feed{id,message,full_picture}');
$postsNode = $responsePosts->getGraphNode();
var_dump($postsNode);

Related

How to automaticaly post on Facebook page with PHP

I'm trying to find solution for automatic posting to my funpage from PHP script, but without user prompt.
My usecase is quite simple - every friday there is event related with my funpage and I would like to post on site wall some information about it (loaded from database) using cron script written in PHP. I would like to use my account to do it.
I found a lot of articles related to autoposting but most of them are stale.
I created new FB application and few lines of code:
$fb = new \Facebook\Facebook([
'app_id' => '1897212530527861',
'app_secret' => $app_secret,
'default_access_token' => $app_access_token,
'default_graph_version' => 'v2.9',
]);
$params = [
'link' => 'www.myurl.com',
'message' => 'Info about the meeting',
];
$response = $fb->post('/my.funpage/feed', $params);
Currently I am reaching error:
The user hasn't authorized the application to perform this action
Might it be related to missing permissions in applciation configuration? I requested for publish_pages and manage_pages, but I can not review it while
there is message.

Facebook Page Tab tabs_added/signed request with PHP

I am revamping the attachment of our Page Tab app using the "Add Page Tab", and am a little unsure about the flow. This is what I'm trying to do:
1) direct our users to the Add Page Tab dialog with our appId (works)
2) our redirect URL is hit with a post that has the "tabs_added" parameter
3) I use this code to try and get information about the FB account that is getting to my app:
$this->fb = new Facebook\Facebook([
'app_id' => <app_id>,
'app_secret' <app secret>,
'default_graph_version' => 'v2.9'
]);
$tabs_added = PARAM('tabs_added');
If I get a 'tabs_added' parameter, I'm assuming that means someone is "attaching" my app to their page, and I want to get information (specifically UID) for their account:
$helper = $this->fb->getPageTabHelper();
$signedRequest = $helper->getSignedRequest();
This call returns nothing
$parsed_signed_request = FacebookSignedRequest::parse_signed_request( $signedRequest, <app secret> );
This call fails because there is no signed request.
What am I missing? I am using v5 of the Facebook PHP API.
thanks

Facebook PHP SDK logout user with endpoint V2.5 not working

I have been trying for hours now to log a user out of Facebook.com using the official php sdk V2.4 (https://github.com/facebook/facebook-php-sdk-v4/) and api endpoint version V2.5.
What I found until now is that I should use
$facebook->destroySession();
However this function is not available if I use
$facebook = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.5',
]);
So instead I used
$helper = $facebook ->getRedirectLoginHelper();
$logoutUrl = $helper->getLogoutUrl($user['facebookAccessToken'], 'www.mypage.com');
This returns
$logoutUrl = https://www.facebook.com/logout.php?next=www.mypage.com&access_token=facebookToken
I checked the token and it is correct. However when I redirect to $logoutUrl then facebook does not logout the user but instead redirects to https://www.facebook.com/home.php while the user is still logged in.
I guess that this is due to the new version V2.5? Is there any way to accomplish this task with the new version?
Thanks a lot in advance!
Ok nevermind, I found out why it didn't work. The problem was that I used xampp on my localhost to debug it. But the redirect link was set to the homepage. After I changed the redirect link to the localhost address it was working.
Hope this helps, if anyone else ever struggles with this.
Cheers

Facebook SDK 5 - reading feeds of a page

I am trying to read the posts of a facebook page, using the actual Facebook SDK 5 for PHP. I have created a facebook app, downloaded and "required" the SDK successfully, but I cannot get the request to run, since it always throws a Invalid OAuth access token signature exception.
The code I have so far:
$fb = new Facebook\Facebook([
'app_id' => $appid,
'app_secret' => $secret,
'default_graph_version' => 'v2.5'
]);
$accesstoken = file_get_contents("https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $appid
. "&client_secret=" . $secret
. "&grant_type=client_credentials" );
//following lines are something I just tried, there have been multiple
//Variantions, none ever worked
$cilent = $fb->getOAuth2Client();
$accessToken = $cilent->getLongLivedAccessToken($accesstoken);
All in all my main issues are:
how to get a valid access token? Since I just want to read public data (feeds of a page), I do not need some special permissons and I am quite confused which on Type of access token I need.
Are there any settings within the facebook app I need to make, or make sure, to get this up and running?
I have googled the web for examples, but I could find examples either using older SDKs (if used at all), or showing how to post data.
I do not have too much experience with that topic and I would appreciate some explanation, or a piece of example.
[UPDATE]
I could manage to get the feeds, some dive through the source was needed.
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.5'
]);
$req = $fb->get(
'/<page-id>/feed?fields=full_picture,message,name,picture,story,posts&limit=20',
$fb->getApp()->getAccessToken());
In case of success everything one is looking for is included in $res. For sake of simplicity I removed the error handling.
Because everything is desinged to read public data, there is no need for any access-token huzzle, however, retrieving user Data, posting or something else, is not possible with this solution.
Cheers!

how do you reuse fb access tokens after the redirect in php

I've just gotten started with the latest facebook SDK (5.0). It's been a number of years since I last used it, and there's one thing I can't seem to get around...
Following the example code they have here: https://developers.facebook.com/docs/php/howto/example_facebook_login/5.0.0
I'm able to get a perfectly working login. My problem is, how do you keep this login alive on the next page load when you aren't being redirected back from facebook?
you used to be able to store the user access token, then reuse that token to keep the session authenticated. In version 5.0 of the SDK, I'm not seeing a way to do that, and i'm pretty lost how you remain logged in.
I wouldn't be asking this here, but I've been looking for a couple days now and have found an abundance of articles using older code that doesn't line up with the current 5.0 sdk I'm trying to use. I'm guessing this is something small that I've been missing or over looking, but I'd love any information you guys can give me.
At the end of the exemple in the link you give, there is this line:
$_SESSION['fb_access_token'] = (string) $accessToken;, so the access token is stored in the PHP session and then accessible from page to page.
You can also store it in your database as a field in your users table.
Below great upgrade guides:
Facebook PHP SDK v3.x to v5.x
Facebook PHP SDK v4.0 to v5
I know it's too late but I just wanted to leave an answer as a record. From FB official doc:
Configuration options
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_access_token' => '{access-token}',
'enable_beta_mode' => true,
'default_graph_version' => 'v2.3',
'http_client_handler' => 'guzzle',
'persistent_data_handler' => 'memory',
'url_detection_handler' => new MyUrlDetectionHandler(),
'pseudo_random_string_generator' => new MyPseudoRandomStringGenerator(),
]);
If you don't provide a default_access_token in the configuration
options, or you wish to use a different access token than the default,
you can explicitly pass the access token as an argument to the get(),
post(), and delete() methods.
$res = $fb->get('/me', '{access-token}');
$res = $fb->post('/me/feed', ['foo' => 'bar'], '{access-token}');
$res = $fb->delete('/{node-id}', '{access-token}');

Categories