Facebook post wall data when facebook offline ( access_token = staticValue ) - php

I am trying to post dynamic data to facebook wall.
I downloaded facebook api for php.
First of all, as for testing, I use below code.
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require '../src/facebook.php';
$config = array(
'appId' => 'myAPPID',
'secret' => 'MySecretCode'
);
//'access_token' => $access_token = $facebook->getAccessToken(),
$facebook = new Facebook($config);
$attachment = array(
'access_token' => 'my_tokenkey',
'message' => 'Test Message offline test message',
'name' => 'Name Test offline test...',
'description' => 'here goes description and links http:anotherfeed.com | http://facebook.com/anotherfeed',
'caption' => 'this is caption for action link',
'picture' => 'http://www.google.com/tv/images/slivetv.png',
'link' => 'www.yahoo.com',
'properties' => array(
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
),
);
try {
$facebook->api('/me/feed', 'POST', $attachment);
} catch(FacebookApiException $e) {
echo $e;
# handling facebook api exception if something went wrong
}
?>
As for my program requirement, I want my program to post data to facebook's wall whether facebook is online or offline.
But when I try this code, I get error.
OAuthException: An active access token must be used to query information about the current user.
Could anyone please give me suggestions?

Related

publicating using php facebook api creates only permalink insted of post on facewall

I'm using PHP facebook API 3.2.3 to connect to facebook and publicate messages from my page.
Until recently it was working find, but now messages are not publicated.
When i copy returned by FB message ID to create link, it is there, but not on my facewall.
I can accesss to my message by link like this:
https://www.facebook.com/permalink.php?story_fbid=800134770033938&id=659754124150164
It says, that i've publicated link on my fanpage, but it is not visible on wall.
Ofcourse there are all necessary permissions.
Code looks like this:
$fb_fanpage_name = $fb['FBFanpageName'];
$fb_access_token = $fb['FBAccessToken'];
$fb_app_id = $fb['FBApp'];
$fb_secret = $fb['FBSecret'];
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => TRUE,
));
$post = array(
'access_token' => $fb_access_token,
'link' => $fb_url
);
try
{
$res = $facebook->api('/' . $fb_fanpage_name . '/links', 'post', $post);
} catch (Exception $e)
{
$this->zp($e->getMessage());
}
According to FB API on page https://developers.facebook.com/docs/graph-api/reference/v2.3/page/links/ - you can't posting links like this.
Apparently You want post message to feed.
So to do this try :
$post = array(
'access_token' => $fb_access_token,
'link' => $fb_url,
'message' => 'try me!'
);
$facebook->api('/'. $fb_fanpage_name .'/feed', 'POST', $post);
Update : to find this look at page https://developers.facebook.com/docs/graph-api/reference/v2.3/page/feed

Post data to facebook page with no user interaction

After I was searching how to post data to facebook page, now I can do it well.
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => '491652477512398',
'secret' => 'de3d98a619ade1afb4152d6be90acdb9',
));
$user_id = $facebook->getUser();
echo "User data ".$user_id." \n";
try {
if($user_id) {
$ret_obj = $facebook->api('/microdigit.it/feed/', 'POST',
array(
//'access_token' => 'AAACEdEose0cBAExkj6B4lnjpUtfSSVTOZA9CuvMA1SDjyCQWRnfZCbc1SimyRsZCTa6CUFZB2Q3ZBfVIv0qJmW13XkPZASpt9UDE4qQI488mDqlznDZA2ih',
'message' => '*** Microdigit Microdigit 000 ****',
'name' => 'name 777 after log out test',
'caption' => 'this is caption for action link',
'description' => 'here goes description and links http:anotherfeed.com | http://facebook.com/anotherfeed',
'picture' => 'http://www.google.com/tv/images/slivetv.png',
'link' => 'www.yahoo.com',
'properties' => array(
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
array('text' => 'test link 1', 'href' => 'http://anotherfeed.com/?ref=1'),
),
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
}else
{
$login_url = $facebook->getLoginUrl( array( 'scope' => 'offline_access,publish_stream' ) );
echo 'Please login.';
}
} catch(FacebookApiException $e) {
echo $e;
$login_url = $facebook->getLoginUrl( array(
'scope' => 'offline_access,publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
?>
But problem is I want to post
that information to page without needing any user interaction.
I mean whenever I run this code, If user is already log off,
my program need user interaction (such as login to facebook) before it post to facebook page.
So I will be appreciated every suggestion that can show me how to post data to facebook without needing any user interaction.
One way of doing it, depending on the trigger you want to use is to make use of a Cron Jobs . You can schedule to run php scripts.

Problem with posting to Facebook Page using PHP script as Page account

I am using the following PHP code to post to my page wall. it is okay I can post successfully as admin but I want to post to the wall using the page name (page account). how do I post using the page name or page id. I tried to find good resource to explain Facebook API function but i did not find good documentation for it.
<?
require '../src/facebook.php';
$app_id = "XXX";
$app_secret = "YYY";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access'))}");
exit;
}
else {
echo "i am connected";
}
$attachment = array('message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => 'http://example.org/image.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789
?>
I found the solution to the problem.
To post or to do any task as a page , you need an access token.
Here is my final code after the update, I want to share it with you because I found many people having difficulty with getting the correct access code for a page.
<?
require '../src/facebook.php';
$app_id = "XXX";
$app_secret = "YYY";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$user = $facebook->getUser();
//echo $user;
if(($facebook->getUser())==0)
{
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}");
exit;
}
else {
$accounts_list = $facebook->api('/me/accounts');
echo "i am connected";
}
//to get the page access token to post as a page
foreach($accounts_list['data'] as $account){
if($account['id'] == 123456789){ // my page id =123456789
$access_token = $account['access_token'];
echo "<p>Page Access Token: $access_token</p>";
}
}
//posting to the page wall
$attachment = array('message' => 'this is my message',
'access_token' => $access_token,
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'example.org',
'description' => 'this is a description',
'picture' => 'http://example.org/image.gif',
'actions' => array(array('name' => 'Get Search',
'link' => 'http://www.google.com'))
);
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789
var_dump($status);
?>
My Facebook OAuth is a little rusty, but I believe this will help:
http://bugs.developers.facebook.net/show_bug.cgi?id=10005
It is the exact bug you are describing, but I have no way of testing that it corrects the issue specifically.
This should help: http://developers.facebook.com/docs/reference/api/page/#feed
It looks like you need the manage_pages permission.

Update Facebook wall with PHP

I have a blog system where I want the user to be able to post on there Facebook wall without them having to login to Facebook. I would like it to happen through the PHP SDK delivered by Facebook.
This is the permissions I have given the Facebook application i created:
https://www.facebook.com/dialog/oauth?client_id=xxxxxxxx&redirect_uri=http://www.mydomain.com/facebook/index/&scope=offline_access,publish_stream,read_stream,manage_pages
Just to clarify the permissions: offline_access,publish_stream,read_stream,manage_pages
Below codes only works if the user is logged in. else I get this error:
FacebookApiException [ 0 ]: An active access token must be used to query information about the current user.
public function facebook()
{
require_once('facebook/php-sdk-3.0.0/src/facebook');
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$attachment = array(
'access_token' => $facebook->getAccessToken(),
'message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://www.google.com',
'description' => 'Test of application PHP',
'picture' => 'http://www.lalibre.be/img/logoLaLibre.gif',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'http://www.google.com')
)
);
$result = $facebook->api('/me/feed/','post',$attachment);
print_r($facebook->getAccessToken());
}
In order to post to someone's wall your application must have wall post permissions for that user. In order to get those permissions the user must login to facebook and grant you access.
For more info: http://developers.facebook.com/docs/authentication/
Also, you need "offline_access" if you want to post when's the user is not logged in.

Facebook graph API & PHP SDK questions

I have a couple of problems using facebook graph api.
1).
When i try to upload a photo i get the following error "(#324) Requires upload file".
$attachement = array(
'access_token'=> (...)',
'name' => 'uploaded foto',
'source' => 'C:\Documents and Settings\Username\Desktop\1.jpg'
);
$fb_foto = $fb->api('me/photos','POST',$attachement);
I am sure that source is correct. I have tried with a photo from the internet and not from mu local PC also.
2). How can i delete an object from facebook?. (a wall message for example).
I have tried this:
$fb->api('/post_id','POST',array('method'=> 'delete'));
But i get "unsupported POST request" error.
Any help would be appreciated.
EDIT: Here is the complete solution i found for upload photos to Facebook. You need to have the version 2.1.1 or above of PHP SDK
$fb = new Facebook(array(
'appId' => ...,
'secret' => ...,
'cookie' => true,
));
$fb->setFileUploadSupport(true);
$attachement = array(
'access_token'=> '...',
'name' => 'uploaded foto',
'source' => '#absolute_path_to_the_file'
);
$fb_foto = $fb->api('me/photos','POST',$attachement);
Concerning your 2nd problem, I remember reading somewhere about a DELETE request, instead of POST. See:
http://developers.facebook.com/docs/api#deleting
Read the documentation
curl -F 'access_token=...' \
-F 'source=#file.png' \
-F 'message=Caption for the photo' \
https://graph.facebook.com/me/photos
See how the source parameter is formatted?
$fb_foto = $fb->api('me/photos','POST',array(
'access_token' => (...)
, 'message' => 'Caption'
, 'source' => '#' . realpath( 'path/to/file' )
));
To delete photos, again the documentation has your answer: Issue a DELETE request
$fb->api( '/PHOTO_ID', 'DELETE' );
I'm using this method with the new php-sdk (v2.1.1, facebook-php-sdk-v2.1.1-0-g08909f3.zip) to upload a photo for a new event. The php code below creates the event.
$fname="/tmp/foo.jpg";
$attachment = array(
'access_token' => $facebook->getAccessToken(),
'name' => substr(event_name),
'description' => my_description,
'start_time' => my_start_time,
'link' => my_link,
'source'=> '#'.$fname
);
$result = $facebook->api('/me/events', 'POST', $attachment);

Categories