php - Post to facebook via cron -
i've been trying 2 days post messages gathered twitter search 1 of facebook pages automatically - i.e. via cronjob.
the twitter part went fine, life of me can't facebook part work.
the problem script works... until doesn't, access_token expired after few hours.
now have message : #200) posts actor page cannot include target_id
.
i've tried many things suggested on various threads. problem is: facebook api seems change quite , used work doesn't.
any idea , suggestion how make work reliably welcome.
here code have far. i've created facebook app, , generated access token using fb graph explorer , request '/me/account'.
require('config.inc.php'); require('_classes/facebook-php-sdk/src/facebook.php'); // connect facebook $facebook = new facebook(array( 'appid' => fb_app_id, 'secret' => fb_app_secret, )); // message $msg_body = array( 'message' => $message->message."\n".'(via http://twitter.com/'.$message->author.')', 'access_token' => fb_access_token ); // post 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.' posted on facebook wall.'; //print_r($msg_body); }
update code visible here http://phpbin.net/zmnt3mpt
i having similar problem access token expiring. turns out can exchange token "long lived" token
managed dig code:
try{ $token = $facebook->getaccesstoken(); // "long-lived" access token $curl = new curl(); $curl->setssl(); $exchange_url = "https://graph.facebook.com/oauth/access_token?client_id=".$facebook_app_id."&client_secret=".$facebook_app_secret."&grant_type=fb_exchange_token&fb_exchange_token=".$token; $page = $curl->get($exchange_url); if ($page){ $page = explode("access_token=", $page); if (count($page) > 1){ $page = explode("&", $page[1]); $token = $page[0]; $facebook->setaccesstoken($token); } } } catch(exception $e){ $token = ''; }
Comments
Post a Comment