Feed Offline Access - php

I have an app that when the user goes to the app, searches his feed for specific posts. (I have read_stream). But i needed the app to read their feed whenever I want, like once an hour. How can i read their feed without they are in the app ?

Every time the user, enters your app you must save is fb_id on your data_base and you must save the extended token. This token will be valid for 60 days, so everytime the user goes to your app you should renew it, this way won't expire.
I don't know if you're using PHP SDK but here's the way you can get the facebook extended token:
require_once("facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config[‘fileUpload’] = false; // optional
$facebook = new Facebook($config);
$facebook->setExtendedAccessToken();
$access_token = $facebook->getAccessToken();
$uid = $facebook->getUser();
//now that you have the extended token and the user id save it on your database.
Now the next time you want to read the feed of the user offline, just grab the user id from the database and
//the next time you want to read the user feed, just grab the uid from your database and access token and set it
$facebook->setAccessToken($new_access_token);

Related

Facebook Pages API Post Without User Login

I have created a FB App to publish posts as page from another site.
Many of the users will be publishing on my page, so I'm trying to create a function without user login or session.
I'm now doing it via creating Page Access Token manually from Graph API Explorer and injecting it into the code.
If I could create the access token in the code without user login, it will be my solution.
Is it possible or anyone knows how to do it?
Any help is appreciated.
P.S: In case anyone wonder my code, it is below.
$config['appId'] = $appId;
$config['secret'] = $secret;
$fb = new \Facebook($config);
$message = array(
'access_token' => $page_access_token, //manually injected
);
$message['message'] = 'message';
$message['link'] = 'link';
$posturl = '/'.$page_id.'/feed';
$result = $fb->api($posturl,'POST',$message);
There is example code in the docs, this is how to post stuff with a specific Access Token:
$response = $fb->post('/{page-id}/feed', $data, '{access-token}');
Source: https://developers.facebook.com/docs/php/howto/example_post_links
Of course you can´t create a Page Token without user login, that would be weird. You can only create a Page Token with a User Token, and you can only get a User Token with login. Extended Page Tokens are valid forever, so you only need to create it once and just store it.
More information about Tokens and how to generate them:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

Codeigniter and facebook php sdk autopost to company wall

I have built a site with codeigniter and implemented facebook login for users. Everything is working great there. App is connected user tokens beig saved the whole nine yards. What I am having trouble doing is having the website itself NOT the logged in user post to the connected company facebook page on a user post completion. essentially the user posts a listing. the website then on submit of that posting posts to my facebook company page NOT the logged in users facebook wall. (i have that working already. Can i leave the website logged into Facebook to post to the wall while a user is logged on as well?
You will need to get the Appid, App secret and an access token. You can extend the access token so that it doesn't expire.
$graphUrl = 'https://graph.facebook.com/oauth/access_token?client_id='.APPID .'&client_secret='.APPSECRET.'&grant_type=fb_exchange_token&fb_exchange_token='.ACCESS_TOKEN;
$accessToken = #file_get_contents($graphUrl);
parse_str($accessToken); //get the access_token param in the string and would be named $access_token
if(!$access_token) $access_token = $accessToken; //if cannot be extended then just return the access token with 2 hours expiry
In order to post as your company page, you will need their page id.
Here is part of my script that I used to get all this working. Its not codeigniter, but you will be able to see how it works.
$config = array(
'appId' => APPID,
'secret' => APPSECRET,
);
$facebook = new Facebook($config);
$facebook->setAccessToken(ACCESS_TOKEN);
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$page_id = '************'; //
$page_info = $facebook->api("/$page_id?fields=access_token");
if(!empty($page_info['access_token']) ) {
// do your code stuff
}
} catch etc etc
Hope this is helpful to you

How to get data from facebook Ads on the Graph API?

I am new to PHP and facebook API'S
I am trying to get data from my ads account on faceobook using PHP
I just want to know how much $ every ads spent
First of all - I did not understand if I must have facebook app in order to get data from my personal ad account ?
I assumed yes so I created one now..,
I am geting the acess token like this :
require_once("facebook.php");
$config = array();
$config['appId'] = 'myapp_id';
$config['secret'] = 'myappsecret';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
// Set a new app secret
$facebook->setApiSecret($config['secret']);
// If you do above, also set the app id
$facebook->setAppId( $config['appId']);
$access_token = $facebook->getAccessToken();
But when I am trying a get request like this:
https://graph.facebook.com/act_[my_account_number]/adgroups
I am getting a premisson exception that tell me
An access token is required to request his resource
But where do I put the access token ?
You need to add it at the end of the url as a url parameter:
"https://graph.facebook.com/act_[my_account_number]/adgroups" . "?access_token=" . $access_token
However, from the code I think you are getting an App Access Token, and you need a Page Access Token or a User Access Token to get the ads related to your Facebook Page.
Please refer to https://developers.facebook.com/docs/facebook-login/access-tokens/ for more information.

Facebook social graph API call for authenticated user

I'm trying to grab data about a user in Facebook that can only be fetched if the app is authenticated (for example /likes).
However, I can't seem to grab this data despite the fact that the user has authenticated the Facebook app (and the applicable specific permissions). I can only get data from a public context.
I'm assuming I must be missing some sort of authentication step, but I can't figure out how. When I call $fb->getAccessToken(), it returns a string value, so it seems like it does get an access token for making requests. I see some solutions to force the user to click on a login URL to authenticate, but hasn't that already been done if they have authenticated the FB dialog previous (via Javascript)?
Besides that, I need the script to run without any input from the user.
Current code:
$fbConfig = array(
'appId' => 'xx',
'secret' => 'xx',
);
//get facebook configuration parameters defined in admin backend
$fb = new facebook($fbConfig);
$access_token = $fb->getAccessToken();
$fbUid = '123456789'; // Known Facebook User Id
$likes = $fb->api('/' . $fbUid . '/likes&access_token=' . $access_token);
var_dump($likes);
// Result is always: array(0) { }
Here you just want to get a likes of user of your application, i m giving you some code which have been created from me and it's working, i test it. you don't have need to get access token because it will be taken at every time.
<?php
set_time_limit(0);
include_once '../src/facebook.php';
$config=array(
'appId'=>"xxxxxxx",
'secret'=>"xxxxxxx",
'cookie'=>true
);
$facebook = new Facebook($config);
$user=$facebook->getUser();
$accessToken=$facebook->getAccessToken();
$likes=$facebook->api('/me/likes');
var_dump($likes);
?>
Here, set time limit=0 because it'll take more time to get a every likes of user.

access graph data without forcing user to login

I am looking to pull data from a fb page to a website without having a user authenticate.
The data I wish to display on a webpage is the event listing for a fb page, both the page and the events are open, and I can grab the page and event data ok, eg
http://graph.facebook.com/PAGEID
http://graph.facebook.com/EVENTID
But to get a list of the events in order to get the eventIDs an access token is required eg
http://graph.facebook.com/PAGEID/events
Is this possible to get this data without having a user login to fb and authorise with an application ?
I have looked at just saving an access token but this seems to expire.
any suggestions/pointers would be great, thanks in advance
Providing a quick solution to this set up an app and get an access token, note it would be best to cache the result data so as to not make an api call on each page load
<?php
//set your app id, client_secret and fb id
$client_id = 'xxx';
$client_secret = 'xxx';
$fbID = 'xxx';
//get an access token
$access_token = file_get_contents("https://graph.facebook.com/oauth/access_token? type=client_cred&client_id=".$client_id."&client_secret=".$client_secret);
//use this access token to return the events
$jsonurl = "https://graph.facebook.com/$fbID/events?$access_token";
$json = file_get_contents($jsonurl,0,null,null);
// do stuff with $json
?>

Categories