I try to build an facebook auto poster for the own users wall (post as user to own wall). Therefor I have to do an facebook application through https://developers/facebook.com/apps
. After I’ve defined the Secure Canvas URL
I wrote the code for the index.php
of the Secure Canvas URL
to have the ability to post messages, links or pictures to the own users wall:
session_start();
require __DIR__ . '/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication('App-ID','App-Secret');
$helper = new FacebookCanvasLoginHelper();
$path = 'path/';
$message = file_get_contents($path . 'message.php');
$description = file_get_contents($path . 'description.php');
$link = file_get_contents($path . 'link.php');
$picture = file_get_contents($path . 'picture.php');
try {
$session = $helper->getSession();
} catch (FacebookRequestException $ex) {
echo $ex->getMessage();
} catch (\Exception $ex) {
echo $ex->getMessage();
}
if (isset($session)) {
try {
// Doing a post
if ($picture == '') {
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'message' => $message,
'link' => $link,
'description' => $description
)
))->execute()->getGraphObject();
// Uploading a picture
} else if ($picture != '') {
$response = (new FacebookRequest(
$session, 'POST', '/me/photos', array(
'source' => new CURLFile($picture, 'image/jpg'),
'message' => $message
)
))->execute()->getGraphObject();
}
} catch (FacebookRequestException $e) {
echo $e->getMessage();
}
} else {
$helper = new FacebookRedirectLoginHelper('https://apps.facebook.com/App-Name/');
$auth_url = $helper->getLoginUrl(array('email', 'publish_actions'));
echo "<script>window.top.location.href='".$auth_url."'</script>";
}
When I’m calling the Secure-Canvas-URL/index.php
everything is working well and the post appears on my wall. So now it was the task to write a php script named cron.php
which calls the index.php
file above to post on my wall without calling the Secure-Canvas-URL/index.php
directly. I’ve done that via cURL:
facebookLogin('email', 'password');
// Function for logging in to facebook via cURL
function facebookLogin($email, $password) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.facebook.com/login.php');
curl_setopt($ch, CURLOPT_POSTFIELDS,'email='.urlencode($email).'&pass='.urlencode($password).'&login=Login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
curl_setopt($ch, CURLOPT_REFERER, "http://www.facebook.com");
$page = curl_exec($ch) or die(curl_error($ch));
// Post on facebook
facebookPost();
}
// Function for posting content through facebook via cURL
function facebookPost() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://Secure-Canvas-URL/');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
$page = curl_exec($ch) or die(curl_error($ch));
}
When I’m calling the php file via http://example.com/cron.php
the post appears on my wall. So this is working well, too. Now the php script has to be run through a cron job for automatically posting:
* * * * * /kunden/usr/bin/wget http://example.com/cron.php
The cron job calls the php script successfully which I tested by sending an email each minute. The problem is that the script cron.php
isn’t able to post on my wall when it’s running by the cron job.
I suspect that I have to receive something from facebook which happens when I’m calling the Secure-Canvas-URL/index.php
or http://example.com/cron.php
in my browser but not when the cron is doing that job.