Post to facebook via cron

2019-04-07 04:44发布

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

2条回答
仙女界的扛把子
2楼-- · 2019-04-07 05:26

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:

try{
        $token =  $facebook->getAccessToken();

        // get "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 = '';
    }
查看更多
Ridiculous、
3楼-- · 2019-04-07 05:38

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

查看更多
登录 后发表回答