Posting to Facebook via Cron and PHP
November 25, 2011 1:26 PM   Subscribe

How do I post to Facebook using a PHP script that is running off a cron job, instead of in my browser?


Ok, so I know there is a lot of information out there on the basic idea but my Googling keeps revealing information that is either contradictory, a bit over my head, or no longer works with the changes to the facebook API over the last year or so. So I'm hoping the kindly hive mind can help hold my hand through this.

Background, so I run a Facebook page for my online t-shirt store, I'm currently running a design marathon where I'm posting a new shirt each day. I have everything about this process automated and running as a cron job except posting to my Facebook page.

To get the Facebook part automated I've set up an app. I've authenticated my account with the app and given it the appropriate permissions (offline_access, manage_pages, and so forth) and thus the script works perfectly *IF* I'm running the script in a browser, but if I run the script as a cron job the server clearly can't authenticate the same way as my browser does so I need to make some changes. Apparently these changes include getting the Session ID (or key?) from when I granted my app offline_access and then passing my session ID/Key as part of the cron-ified script.

My first problem is that I can't seem to figure out how to get that session key in the first place. I've tried a few methods that people have posted, but it seems like the three methods I've tried are for older versions of the Facebook api and are no longer supported.

My second problem is that even when I get the session key I'm not actually sure how / when / where to pass it along in the script.

Can anyone walk me through these two steps? Thanks much!

Oh, here is my current code for reference. This is the script that runs fine via browser, but can't run off a cron:


$app_id = "xxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$your_page_id = 'xxxxxxxxxx';

$fb = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true

//get the access token to post to your page via the graph api
$accounts = $fb->api("/me/accounts");
foreach ($accounts['data'] as $account)
if ($account['id'] == $your_page_id)
//found the access token, now we can break out of the loop
$page_access_token = $account['access_token'];

//publish a story to the page's wall (as the page)
$post_id = $fb->api("/{$your_page_id}/feed", "POST", array(
'message' => $MY_AWESOME_MESSAGE,
'link' => $MY_AWESOME_URL,
'access_token' => $page_access_token,
catch (Exception $e)

And this was my initial authentication script that I used to authorize access to my app with my FB account:


$app_id = "xxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$fb = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true

$params = array(
scope => 'read_stream,publish_stream,offline_access,manage_pages',
redirect_uri => ''

$loginUrl = $fb->getLoginUrl($params);

else { echo "Connected to Facebook"; }
posted by Jezztek to Computers & Internet (7 answers total)
I'm not exactly sure what you want to post on Facebook, but there's a possibility you might be able to do this with Ifttt (If This, Then That).

You could set up a recipe with Gmail that looks for any message from a specific address, or with a specific label, and use the Facebook channel to post a new link or status message (depending on what you're after). You'd just need your PHP script to e-mail the link information to a Gmail account (if you don't have one, you can create one) instead of trying to post it to Facebook directly.

I don't have a Facebook account so I'm not really up on what a Link Post is vs a status message vs publishing a story to your page's wall, but I hope that helps.
posted by backwards guitar at 1:39 PM on November 25, 2011 [4 favorites]

Backwards Guitar - Ah, I've never heard of Ifttt before, I look over its documentation and see if that fits the bill.

Oh, I forgot to add on the above post that I've run through the process listed here and received my access_token, but I honestly have no idea if that's the same thing as a session key (which is what several of the tutorials [possibly outdated] dealing with this general issue mentioned)
posted by Jezztek at 1:50 PM on November 25, 2011

I'm not sure the exact issue, but have you tried poking around the working script with Firebug and tamper data? With a POST request, the browser sends along a fair amount of information in the HTTP headers (e.g., cookies) that the cron job may not.

Try using those tools to see all the information that's being sent to facebook via the working script in the browser, in order to better mimic it with your standalone script.
posted by losvedir at 3:18 PM on November 25, 2011

Ooooh, I've never seen tamper data before. I'll check that out.

backwards guitar - It looks like Ifttt will do what I need it to. It drives me crazy that I wasn't able to figure out how to do everything on my end, but hey if this works it works. Thanks much!
posted by Jezztek at 12:13 AM on November 26, 2011

Depending on how comfortable you are programming, you can create a facebook application with 'offline access' using OAuth to post to your (or anyone else's) feed. The graph API is pretty straight forward.
posted by delmoi at 3:25 AM on November 26, 2011

Hey Delmoi, that exactly what I'm trying to accomplish but have hit a roadblock. You can see above that I've registered my app and authorized it with offline_access. I've stored the extended offline_access access token but for whatever reason it won't authenticate unless I run the script through a browser, but not directly on my server via cron.

The graph API documentation does a great job of running you through the authentication flow if you are using a browser, but I can't seem to find any decent information on using offline_access without a browser being involved. I know its possible, and I feel like the solution is probably pretty straight forward I just can't seem to find the right info.
posted by Jezztek at 11:02 AM on November 26, 2011

Whew, I finally solved my issue, I'll post it here in case someone else has the same problem.

All I needed to do was get the extended access token as shown in the authentication documentation here then add this line to my php right after initializing the new fb object:


Seems absurdly simple now, but the problem was made complicated by the fact that apparently they made changes to the API last year, and all of the solutions I had been Googling were apparently from the old versions, leading me on several wild goose chases.

Thanks much, everyone.
posted by Jezztek at 11:47 AM on November 26, 2011

« Older Keeping a new relationship with med school...   |   Can we move to the Bay Area? SHOULD we? Newer »
This thread is closed to new comments.