I know that there are lot's of questons on this, but all seem to be needing a user to be logged in... I have been using code snippets from all possible tutorials, but none seem to work.
Here is the scenario: I have a photo community running on PHP and I have a fan page on Facebook. When an image on the main site collects a certain amount of votes a function is triggered to post the image link to the Facebook wall. The link (post) is posted as a page and not as admin. Admin of course will not be online... Is this even possible to do these days? I have the latest PHP SDK and this is the function that I need to get working in stand alone mode before pluggin' into the main site.
OK. This code works perfectly if I am logged into the Facebook, but if I am not - it will not post... The App has all necessary and unnecessary :) permissions to interact with my page on my (admin) behalf. Any ideas will be appreciated.
<?php
//facebook application
$fbconfig['appid' ] = "1848740815xxxxx";
$fbconfig['secret'] = "a5aa62bb3a8ddcb98d5d9dbe4a3xxxxx";
$fbconfig['pageid'] = "121409594622865";
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret']
));
//Facebook Authentication part
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'offline_access,publish_stream'
)
);
$logoutUrl = $facebook->getLogoutUrl();
$pageid = $fbconfig['pageid'];
if ($user) {
try {
$page_info = $facebook->api("/$pageid?fields=access_token");
if( !empty($page_info['access_token']) ) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'This is a test feed message',
'link' => 'http://www.fotodvor.com',
'picture' => 'http://www.fotodvor.com/data/media/15/1319971991.jpg',
'name' => 'Test Picture',
'description'=> 'Description of the test picture!'
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
Thanks in advance
here is a modified script with explanations adjusted for the changes in the API
You should be using the page access token if i'm understanding what you're trying to do correctly - this is a token which allows your app to act as the page, not as one of the admins. This is accessible at the
/me/accounts
endpoint when you have a user access token withmanage_pages
permission - if you use that access token to post to/{page id}/feed
(or photos, etc) it'll appear as the pageI think I have found the answer. After a long time of testing here is the solution: Treat this as a little sample/guide for those who are new to this. The code and output has all necessary info: