Php – programmatically login to facebook and post to page (wall)

facebookfacebook-graph-apiPHP

I have a website, and I need it to post status updates to a Facebook Page from time to time.

Using my personal Facebook account, I created an App, and a Page. So far, I've been able to programmatically post to my Page's Wall, by adding this code to my website:

include_once "lib/facebook/src/facebook.php";

$facebook = new Facebook(array('appId' => 'APP_ID_HERE', 'secret' => 'APP_SECRET_HERE'));

if($facebook->getUser()) {

    try {
        $ret_obj = $facebook->api('/FACEBOOK_PAGE_ID_HERE/feed', 'POST', array(
            'link' => 'www.example.com',
            'message' => 'Posting with the PHP SDK!',
            'access_token' => 'FACEBOOK_PAGE_ACCESS_TOKEN_HERE'
        ));
        echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

    } catch(FacebookApiException $e) {
      // user logged out (has user_id, but invalid access token)
      $login_url = $facebook->getLoginUrl(array('scope' => 'publish_stream')); 
      echo 'Please <a href="' . $login_url . '">login.</a>';
    }
    echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';

} else {

    $login_url = $facebook->getLoginUrl(array('scope' => 'publish_stream'));
    echo 'Please <a href="' . $login_url . '">login.</a>';

}

So I just open my website, click "Please login", login as myself. Once I'm logged it, it will now be able to post the status update to the Facebook Page.

Obviously, the problem here is that I need to be logged in for it to be able to post. If other users will try to login with their user accounts, my website cannot post status updates to the Facebook Page because I am the only Admin for the app/Page.

My question is, is there a way for me to programmatically log myself into Facebook so I can do these status updates to my Page automatically?

Sorry, total noob here to Facebook development.

Best Answer

Seems like you need to update status while you are offline from facebook. To do this you need to get the offline access permission of the app user and you need to have a "infinite" token for facebook app to access the APIs, so that your program can update anytime you want with out logging into facebook.

You may get something you want here

http://developers.facebook.com/docs/authentication/

Related Topic