I am working on a script to pull recent Facebook posts/status updates for a specific using the Facebook API. I downloaded the FB SDK and searched all over the FB developers site, SO, and other places and it seems like the code below should accomplish what I need. However, although I am not getting an error, I am getting a NULL result.
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
));
$fbApiGetPosts = $facebook->api('***My Name***/feed?limit=5');
var_dump($fbApiGetPosts["data"]);
I have tried using my two FB accounts,(my personal acct and a test account that I created just for this project) as well as one for my company and each time the response was NULL. When I misspelled the account name or used a name that did not exist I got an error, which seems to suggest the request is going somewhere.
In the code above, I also tried var_dump-ing $fbApiGetPosts and that result was also null
Can anybody see what the missing piece of the puzzle is?
I've not used the Facebook SDK, but here is how I retrieve Facebook posts:
$appId = 'XXXXXXXXXXXX';
$token = 'XXXXXXXXXXXXXXXXXXXXXXXX';
$posts = json_decode(
file_get_contents('https://graph.facebook.com/' . $appId . '/feed?
access_token=' . $token
)
);
var_dump($posts->data);
Hopefully this helps!
EDIT:
The token is just called an "Access Token". Here are the instructions for obtaining one. You might find the Access Token Tool helpful, but it's more for debugging existing tokens then creating new ones.
Just use the PHP SDK :
download link : https://developers.facebook.com/docs/php/gettingstarted/
Log your app :
$facebook = new Facebook(array('appId' => 'XXXXX','secret' => 'XXXXX'));
$facebook->setAccessToken('stored access token');
then :
$fbApiGetPosts = $facebook->api('/me/feed');
print_r($fbApiGetPosts);
Here is the Graph doc : https://developers.facebook.com/docs/graph-api/reference/
and the /feed ref : https://developers.facebook.com/docs/graph-api/reference/user/feed/
Related
So I want to implement Facebook login on my site. I have dowloaded the SDK, created an app and pasted the app id and secret in.
The problem I am getting is that, while the login flow appears to work and I am getting values returned ok from
facebook->getUser()
and
facebook->getAccessToken()
Calls to
$user_profile = $facebook->api('/me','GET')
throw an exception saying I have no access token.
Since this is unmodified example code straight from the SDK I'm kind of at a loss - if the coding example given doesn't work how am I supposed to figure anything out? :)
Same issue with the code on this page:
https://developers.facebook.com/docs/php/howto/profilewithgraphapi/
Basically that does the same thing but handles the exception so I go round and round in a "login to facebook" loop.
I can see the app added to the list in my facebook profile OK when I log in so that side appears to be working. It seems that either the SDK is broken or the access token I'm getting back is for some reason not valid.
Any help or pointer as to what I've missed would be much appreciated!
Thanks
Justin
Try this
$config['cookie'] = true;
and relogin.
This is from the PHP code I use to login.
Here is the complete flow of the code hope this helps.
Say the page for the FB connect is login.php and I will have the following PHP code on the top of the page
require_once 'src/facebook.php'; //include the facebook php sdk
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_APP_SECRET,
'cookie' => true,
'domain'=> FACEBOOK_CONNECT_DOMAIN
));
$user = $facebook->getUser();
if($user){
// lets set the access token before making a api call.
$new_access_token = $facebook->getAccessToken();
$facebook->setAccessToken($new_access_token);
$user_profile = $facebook->api('/me','GET')
......
......
}else{
// display the fb login
}
IDEA: Get event information without user interaction from various facebook pages (clubs,bars,etc..). However some pages have set that info not to be public.
WORKING PRINCIPLE: I have the latest facebook PHP-SDK on my site and an APP on facebook, so usually i call
$facebook = new Facebook(array(
'appId' => '387262991341732',
'secret' => '09014d999f6e34d80ca3e62e331834cc',
));
$events = $facebook->api("$page_url/events");
PROBLEM:
When a page is not public I have to use an access_token.
1) This is an APP access-token, which is not permitted to view the events.
$app_token = $facebook->getAccessToken();
So I figure I need to get user access_token and add more code:
if (!$user) {
$args['scope'] = 'offline_access';`<br>`
$loginUrl = $facebook->getLoginUrl($args);`<br>`
}
<?php if (!$user): ?>
Login with Facebook
<?php endif ?>`
I click the link by myself and use getAccessToken() to get the user access_token:
$user_token = $facebook->getAccessToken();// I store this in my database
I check this with:
file_get_contents('http://graph.facebook.com/privatepage/events?access_token=$user_token');
// Everything works, im very happy
2) Now I delete all cookies, etc and try to use $facebook->setAccessToken($user_token) and then $events = $facebook->api("$page_url/events"); and get nothing. I assure you one more time that AccessToken is valid.
After quite a long research im stuck with this.
???) *Any Ideas how use $facebook->api(...) and not file_get_contents?
I created a PHP script a year ago, that requests the events from a Facebook page, and it still works fine- except for one page and only since recently.
$config = array();
$config[ 'appId' ] = '468554113184955';
$config[ 'secret' ] = 'APP_SECRET';
$config[ 'fileUpload' ] = false; // optional
$facebook = new Facebook( $config );
$res = $facebook->api( '/Stadtpalais/events?fields=id,name,start_time,description,updated_time,picture.type(large)', 'GET' );
I also tried the request with another app ending with the same result, so I think retrieving a new app secret wouldn't help.
The script is just working with appId and secret, but without any user login. Try the Graph explorer with any app access token like '468554113184955|APP_SECRET' (use your own ID and secret) to reproduce the error. Change the page to any other page and it will work.
Can page admins prevent these public requests through settings? (I haven't found one yet.)
Could an event title or description or whatever trigger an error so that Facebook sends an empty array?
Some time ago I read that Facebook might force a user login to request the events, but since it's working fine with other pages, I'd think that hasn't happened yet?
Thanks in advance!
I can now answer my own question: It's due to the page setting
Age Restrictions = Alcohol-related
I'm working on a problem for many days now without solution.
What I got :
A MySQL database with 1000+ advices
A daily advice displayed on a page with a PHP script
A FB Application + FB SDK
A FB user account
What I'm looking for :
A daily post from my FB account containing the daily advice
The best result I got so far :
A php page which post the daily advice by clicking on a button then login in FB.
Is it possible to save the login informations in the PHP script to get a fully automatic daily post with cron ?
Does anyone know a solution ?
Sincerly,
Ok folks, I did it.
Here is my solution :
1) Download the Facebook PHP SDK from https://github.com/facebook/facebook-php-sdk
2) Generate an extended access token with this code (generate_token.php) :
require_once('src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'YOUR APP ID',
'secret' => 'YOUR APP SECRET ID',
'fileUpload' => true
));
$facebook->setExtendedAccessToken();
$access_token = $_SESSION["fb_".$fb_appId."_access_token"];
$facebook->setAccessToken($access_token);
$accessToken = $facebook->getAccessToken();
The $accessToken is a 60 days long access to your account by the application.
Store this token in your database.
3) Publish script (fb_post.php):
Get the $accessToken from your database, then use it to access to your account without manuel login :
require_once('src/facebook.php');
$app_id = 'YOUR APP ID';
$app_secret = 'YOUR APP SECRET ID';
$config = array(
'appId' => $app_id,
'secret' => $app_secret,
);
$facebook = new Facebook($config);
$facebook->setAccessToken($fbAccessToken);
$user_id = $facebook->getUser();
Use the first page (generate_token.php) once every 2 months to generate a 60 days token.
"Et voila", just configure a daily Cron Job which execute "fb_post.php" and your robotic Facebook will rise !
did you try facebook SDK for PHP, it is simple you have to register your app with facebook and use your application id and application secret following link contains a sample script.
developers.facebook.com/docs/php/howto/postwithgraphapi
1.You need your Website to set as an Facebook App
2.read into https://developers.facebook.com/docs/opengraph/
Basicly it is possible of course. But it for me it needs time to get used to the Graph Api.
means a quick answer for your desire is not possible
I want to post to facebook page wall as a page using PHP. I've got access_token by below links.
https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=123456789&redirect_uri=http%3A%2F%2Fmysite.net&scope=publish_stream,manage_pages,offline_access
https://graph.facebook.com/me/accounts?access_token=...
I'm using this simple code:
$appid = "";
$secret = "";
$pageid = "";
$access_token = "";
require_once("facebook-php-sdk/src/facebook.php");
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $secret
));
try {
$args = array(
'access_token' => $access_token,
'message' => 'Test',
'link' => 'http://www.test.com',
'description' => 'Test'
);
$post_id = $facebook->api("/$pageid/feed","post",$args);
} catch (FacebookApiException $e) {
error_log($e);
}
And that's the error I'm getting:
OAuthException: (#200) Posts where the actor is a page cannot also include a target_id
But posting /me/feed won't work neither. All solutions I've googled don't work anymore, official documentation didn't help. I got it working when posting as a user (not a page) and with javascript api (required 'share' click action by a user).
Anyone knows the solution for automatic post to a fb page as a page? Spent couple of frustrating days trying to figure it out -_-
Thanks,
A.
I've had the same problem. The reason was that I was an admin of more than one page, and I tried to post with the token of another page.
Pages cannot post to user's walls (that's why you're getting the OAuthException that says "as a page, you cannot set the target_id"). You'll have to first create an application and get the user's permission (publish_stream) to post to their feed via said app.
I assume you have some knowledge of app and how posting work.
1.You have to get Page access token and use this on array.
2.you use this
"/$pageid/feed"
try use this
$facebook->api($pageID . '/feed','POST'
Thanks...