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?
Howdy,
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:
include_once("facebook.php");
$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'];
break;
}
}
try
{
//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)
{var_dump($e);}
And this was my initial authentication script that I used to authorize access to my app with my FB account:
include_once("facebook.php");
$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 => 'https://mydomain.com/fb_auth.php'
);
$loginUrl = $fb->getLoginUrl($params);
if(($fb->getUser())==0)
{
header("Location:{$loginUrl}");
exit;
}
else { echo "Connected to Facebook"; }
>
posted by Jezztek to computers & internet (7 answers total)
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]