张贴在Facebook上通过cron作业(post on Facebook through cron

2019-09-28 14:33发布

我尝试建立了自己的用户墙上的Facebook的自动海报(后为用户拥有墙)。 为此我要做通过Facebook应用https://developers/facebook.com/apps 。 我已经定义了后Secure Canvas URL我写的代码index.php中的Secure Canvas URL不得不发帖,链接或图片到自己的用户墙的能力:

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>";
}

当我打电话的Secure-Canvas-URL/index.php一切运作良好,并在帖子出现在我的墙上。 所以,现在是写一个PHP脚本命名任务cron.php它调用index.php上面的文件张贴在墙上,而不调用Secure-Canvas-URL/index.php直接。 我做的是通过卷曲:

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));

}

当我打电话通过PHP文件http://example.com/cron.php的帖子出现在我的墙上。 因此,这是工作也很好。 现在的PHP脚本必须通过自动张贴cron作业运行:

* * * * * /kunden/usr/bin/wget http://example.com/cron.php

cron作业成功调用PHP脚本,我通过发送电子邮件,每分钟测试。 问题是,该脚本cron.php不能张贴在墙上时,它是由cron作业运行。

我怀疑,我不得不从Facebook的时候我打电话恰好收到东西Secure-Canvas-URL/index.phphttp://example.com/cron.php在我的浏览器而不是在cron是这样做工作。

文章来源: post on Facebook through cron job