0

I made this PHP script that fetches messages to be posted at regular intervals to a Facebook Page that I administer.

My script is working fine, but depends on me as a user being logged into Facebook. I would like this script to run as a cron job so i fear the script will fail to work if i'm logged out.

How can i make it work even if i'm not logged into Facebook?

Note that I created a Facebook application as requested to be able to use the Facebook api. Is there a way to post as the Facebook application perhaps?

Here is my code, in case it helps.

require_once('config.inc.php');
require '_classes/facebook-php-sdk/src/facebook.php';


$homeurl = 'http://domain.com/to-facebook/index.php';
$post_url = '/'.PAGEID.'/feed';



// The message
        $msg_body = array(
        'message' => 'hello world',
        );

// Post to Facebook

$facebook = new Facebook(array(
        'appId'  => FB_APP_ID,
        'secret' => FB_APP_SECRET,
    ));


// Get User ID
$fbuser = $facebook->getUser();
if ($fbuser) {
    try {
        $postResult = $facebook->api($post_url, 'post', $msg_body );
    } catch (FacebookApiException $e) {
        echo $e->getMessage();
    }
}else{
 // I would like never to come to this point...
    $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>'publish_stream,manage_pages,publish_actions,status_update,offline_access'));
    header('Location: ' . $loginUrl);
}
if($postResult)
{

    echo '<h1>Your message is posted on your facebook wall.</h1>';
}

* update * I tried the cronjob and unfortunately, after a while I had the "Error validating access token: Session has expired at unix time" message. I'm puzzled. What am i doing wrong?

pixeline
  • 17,669
  • 12
  • 84
  • 109

3 Answers3

0

For sites connected to your app: When you have the app get permission to manage_pages, you run an api with your user token which gives you the access tokens of all your pages. These tokens can last forever.

For users authorizing the app: You can make the access_token last longer. The user would need to log in to refresh the extended token however. Read up more about "Facebook Extend Token".

There used to be a permissin that gave the app the ability to reuse the token forever, but it's no longer available. The best you can do nowadays is to simply run the api to extend the token each time the user log in to your app.

Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
  • Are you sure? I was about to publish an answer based on this : http://stackoverflow.com/questions/8231877/facebook-access-token-for-pages And it worked AFAICS, i tried the publishing url from a browser in which i am not logged into Facebook. – pixeline Jul 20 '13 at 19:25
  • Access tokens for sites are ever lasting, but not for specific users. – Robin Castlin Jul 20 '13 at 19:27
  • Sorry, I just missread the content. Am looking from my phone atm :p will correct myself – Robin Castlin Jul 20 '13 at 19:29
0

**update ** I tried the cronjob and unfortunately, after a while I had the "Error validating access token: Session has expired at unix time" message. I'm puzzled. What am i doing wrong?


I seem to have found a solution based on this answer Facebook Access Token for Pages . It explains how to get an access_token to a specific page for your FB application. I obtained it, then adapted my code. I tested it using a browser in which i'm not logged into facebook with, and it works.

Here is the updated script in case it helps.

$msg_body = array(
    'message' => $message->message.' v/ @'.$message->author,
    'access_token' => FB_ACCESS_TOKEN
);



try {
    $postResult = $facebook->api($post_url, 'post', $msg_body );
} catch (FacebookApiException $e) {
    echo $e->getMessage();
}

if($postResult)
{
    echo '<h1>Your message is posted on your facebook wall.</h1>';
    print_r($msg_body);
}

I'll run the cronjob for a few days and will get back to you if it proves not to work reliably.

Community
  • 1
  • 1
pixeline
  • 17,669
  • 12
  • 84
  • 109
0

You can easy use CURL for this. Google something like that, "CURL login, CURL facebook login"

berzins92
  • 55
  • 8
  • Best practice is to use the Facebook php sdk (https://github.com/facebook/facebook-php-sdk), which, underneath, does the curl request. But you're still far from addressing my question with that. – pixeline Jul 20 '13 at 20:24
  • No iam not. With curl you can loggin on site. Then get content, find your form, preg match it and post data. – berzins92 Jul 20 '13 at 20:27
  • One way or another, the problem is to be able to post without being logged in as a user. Meaning: how to get an access token. See my answer for the details. Thank you. – pixeline Jul 20 '13 at 21:09
  • How you can post data, without posting your user ID and session. You need to be logged in. Usr Curl for bot login and posting – berzins92 Jul 21 '13 at 10:14
  • tried that, facebook immediately sent me warnings that someone was trying to access my account. didn't feel like the right way. – pixeline Jul 21 '13 at 12:25