I've been trying for two days to post messages gathered from a twitter search to one of my facebook pages automatically - i.e. via a cronjob.
The twitter part went fine, but for the life of me i can't get the Facebook part to work.
The problem is that my script works... until it doesn't, usually the access_token is expired after a few hours.
Now i have this message :
#200) Posts where the actor is a page cannot also include a target_id
.
I've tried many things suggested on various SO threads. Problem is: the Facebook API seems to change quite often and what used to work doesn't.
Any idea and suggestion as to how to make it work reliably is welcome.
Here is the code I have so far. I've created a facebook app, and generated an access token using the FB Graph Explorer and a request to '/me/account'.
require('config.inc.php');
require('_classes/facebook-php-sdk/src/facebook.php');
// Connect to facebook
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_APP_SECRET,
));
// get the message
$msg_body = array(
'message' => $message->message."\n".'(via http://twitter.com/'.$message->author.')',
'access_token' => FB_ACCESS_TOKEN
);
// Post to Facebook
$fb_result=0;
try {
$postResult = $facebook->api('/'.PAGEID.'/feed', 'post', $msg_body );
} catch (FacebookApiException $e) {
echo $e->getMessage();
}
if($postResult)
{
$fb_result=1;
$last_posted_tweet_id = $message->id;
file_put_contents(FOLDER.LAST_TWEET_ID_FILE, $last_posted_tweet_id);
echo 'Your message '.$message->id.' is posted on your facebook wall.';
//print_r($msg_body);
}
UPDATE Code is visible here http://phpbin.net/ZMNt3MPt
I was having a similar problem with the access token expiring. Turns out you can exchange your token to "long lived" token
Managed to dig up my code:
They are other alternative you can use, for me i find it easier to use Twitter Api to post comment on twiiter and facebook at the same time. I linked it to Facebook, it works great all you will just have to do is change the twitter api key and supply data. If you are interested in this solution let me know and i will post the code here