使用cronjob将Facebook PHP帖子发布到粉丝页面?

使用cronjob将Facebook PHP帖子发布到粉丝页面?,php,facebook,cron,Php,Facebook,Cron,我正在使用以下代码发布到我的facebook粉丝页面,它工作正常。现在我想使用cronjob发布到Facebook。我知道我必须使用作为访问令牌,但我不确定如何设置它。我尝试使用echo我自己的访问令牌和页面的访问令牌,并在post api中使用它,但没有成功,我收到了以下错误: Uncaught OAuthException: An active access token must be used to query information about the current user. 以下

我正在使用以下代码发布到我的facebook粉丝页面,它工作正常。现在我想使用cronjob发布到Facebook。我知道我必须使用作为访问令牌,但我不确定如何设置它。我尝试使用
echo
我自己的访问令牌和页面的访问令牌,并在post api中使用它,但没有成功,我收到了以下错误:

Uncaught OAuthException: An active access token must be used to query information about the current user.
以下是我尝试过的代码:

require_once('scripts/facebook.php');
    $config = array('appId' => 'xxx','secret' => 'xxx');

    $params = array('scope'=>'user_likes,publish_actions,email,offline_access,publish_stream,manage_pages');
    $facebook = new Facebook($config);
    $user = $facebook->getUser();
    if($facebook->getUser()) {
    try {

        $user_profile = $facebook->api('/me');
        $access_token = $facebook->getAccessToken();
        //echo "1. ".$access_token;

      } catch(FacebookApiException $e) {
                        $login_url = $facebook->getLoginUrl($params);
                        error_log($e->getType());
                        error_log($e->getMessage());
      }   
    } else {
        $login_url = $facebook->getLoginUrl($params);

    }    

$page_id = "xxxxxxxxxxxxx";
            $page_access_token = "";
            $result = $facebook->api("/me/accounts");
            foreach($result["data"] as $page) {
                if($page["id"] == $page_id) {
                $page_access_token = $page["access_token"];
                //echo '<br>';
                //echo "2. ".$page_access_token;
                break;
                    }
                }


        $args = array(
            'access_token'  => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'message'       => stripslashes($image_caption).$animaged_gif,
            'name' => stripslashes($image_caption).$animaged_gif,
            'link' => "http://www.example.com/images.php?i=".$image_name,
            'picture' => "http://www.example.com/thumbnails/".$image_name.".png",
            'actions' => array(
            'name' => 'See Pic',
            'link' => "http://www.example.com/images.php?i=".$image_name
            )

        );
       $post = $facebook->api("/$page_id/feed","post",$args);
require_once('scripts/facebook.php');
$config=array('appId'=>'xxx','secret'=>'xxx');
$params=array('scope'=>'user_like,publish_actions,email,offline_访问,publish_stream,manage_page');
$facebook=newfacebook($config);
$user=$facebook->getUser();
如果($facebook->getUser()){
试一试{
$user_profile=$facebook->api('/me');
$access_token=$facebook->getAccessToken();
//回显“1”。$access\u令牌;
}捕获(FacebookApiException$e){
$login\u url=$facebook->getLoginUrl($params);
错误日志($e->getType());
错误日志($e->getMessage());
}   
}否则{
$login\u url=$facebook->getLoginUrl($params);
}    
$page_id=“xxxxxxxxxxxx”;
$page_access_token=“”;
$result=$facebook->api(“/me/accounts”);
foreach($result[“data”]作为$page){
如果($page[“id”]==$page\u id){
$page_access_token=$page[“access_token”];
//回声“
”; //echo“2.”$page\u access\u令牌; 打破 } } $args=数组( “访问令牌”=>“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”, 'message'=>stripsalashes($image\u caption)。$animaged\u gif, 'name'=>stripslashes($image\u caption)。$animaged\u gif, '链接'=>“http://www.example.com/images.php?i=“$image_name, '图片'=>“http://www.example.com/thumbnails/“$image_name.”.png“, “操作”=>数组( '名称'=>'见图片', '链接'=>“http://www.example.com/images.php?i=“$image\u名称 ) ); $post=$facebook->api(“/$page\u id/feed”,“post”,$args);
正如您所看到的,我试图使用在回显自己的访问令牌时获得的实际访问令牌。但这并不奏效。我还使用了页面的访问令牌,但这也不起作用。你能告诉我这里遗漏了什么吗?或者正确的方法是什么?
在进一步搜索之后,我发现了
setAccessToken
page\u info=$fb->api(“/”$sInfo['pageId']。“?fields=access\u token”)但是关于如何使用它们的资源非常有限。我甚至不确定这些是否适合这个问题。我只需要知道我需要使用哪个访问令牌,以及设置它的适当代码是什么?

这可能会解决它

不推荐脱机访问。它现在被一个系统所取代,在这个系统中,您可以获得一个扩展的到期访问令牌

当您手动运行它时,会生成一个实时访问令牌,该令牌在您实际运行它1-2小时后无效。而且,您无法使用cron生成另一个访问令牌。应手动生成访问令牌

因此,要修复它,请阅读:

并落实这一点:

并将访问令牌保存到数据库中。运行cron时,从数据库中获取令牌

p.S:access_令牌有效期为60天,应该再次扩展,用户可以手动登录。访问令牌通常“可能”不会更改,只会延长有效期。

添加:

安排发布时间: 你也可以安排长达6个月的帖子。读了这篇文章,我想我会指出


阅读:向下滚动到帖子创建部分

这是您可以做到的:

  • 获取短期访问令牌
  • 扩展它
  • 获取长寿命页面访问令牌。(没关系,在这里我们每次都要获取新的accesstoken。不那么复杂,而且可靠。)
  • 我想我会使用一个algo,但是我添加了所有可能的代码和文档,因为这可能也会帮助其他人

    获取短期访问令牌并扩展它:

    fetchtoken.php

    <?php
    
    //read more : https://developers.facebook.com/docs/howtos/login/server-side-login/
    session_start();
    $app_id = "xxxxxxxxxxxxxx";
    $app_secret = "xxxxxxxxxxxxxxxx";
    $my_url = "www.stackoverflow.com/";  // redirect url
    
    $code = $_REQUEST["code"];
    
    if(empty($code)) {
        // Redirect to Login Dialog
        $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
        $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
           . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
           . $_SESSION['state'] . "&scope=publish_stream,read_friendlists,email";
    
    
        echo("<script> top.location.href='" . $dialog_url . "'</script>");
    }
    if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
        $token_url = "https://graph.facebook.com/oauth/access_token?"
           . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
           . "&client_secret=" . $app_secret . "&code=" . $code;
    
         $response = file_get_contents($token_url);
         $params = null;
         parse_str($response, $params);
         $longtoken=$params['access_token'];
    
    
    //save it to database   
    } 
    ?>
    
    <?
    require_once('scripts/facebook.php');
    // Pull access token from the file/db
    $access_token = "access token from file or db";
    $facebook->setAccessToken($access_token); // sets our access token as the access token when we call something using the SDK, which we are going to do now.
    
    $config = array('appId' => 'xxx','secret' => 'xxx');
    
    $params = array('scope'=>'user_likes,publish_actions,email,publish_stream,manage_pages');
    $facebook = new Facebook($config);
    $user = $facebook->getUser();
    if($facebook->getUser()) {
        try {
    
            $user_profile = $facebook->api('/me');
        } catch(FacebookApiException $e) {
            $login_url = $facebook->getLoginUrl($params);
            error_log($e->getType());
            error_log($e->getMessage());
        }   
    }
    else {
        $login_url = $facebook->getLoginUrl($params);
    }    
    
    $page_id = "xxxxxxxxxxxxx";
    $page_access_token = "";
    $result = $facebook->api("/me/accounts");
    foreach($result["data"] as $page) {
        if($page["id"] == $page_id) {
            $page_access_token = $page["access_token"];
            //echo '<br>';
            //echo "2. ".$page_access_token;
            break;
        }
    }
    
    $args = array(
        'access_token'  => $page_access_token,
        'message'       => stripslashes($image_caption).$animaged_gif,
        'name' => stripslashes($image_caption).$animaged_gif,
        'link' => "http://www.example.com/images.php?i=".$image_name,
        'picture' => "http://www.example.com/thumbnails/".$image_name.".png",
        'actions' => array(
                'name' => 'See Pic',
                 'link' => "http://www.example.com/images.php?i=".$image_name
        )
    );
    $post = $facebook->api("/$page_id/feed","post",$args);
    ?>
    
    
    
    因此,您现在在数据库、文本文件或其他文件中准备了一个长寿命的accesstoken

    cronjob.php

    <?php
    
    //read more : https://developers.facebook.com/docs/howtos/login/server-side-login/
    session_start();
    $app_id = "xxxxxxxxxxxxxx";
    $app_secret = "xxxxxxxxxxxxxxxx";
    $my_url = "www.stackoverflow.com/";  // redirect url
    
    $code = $_REQUEST["code"];
    
    if(empty($code)) {
        // Redirect to Login Dialog
        $_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
        $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
           . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
           . $_SESSION['state'] . "&scope=publish_stream,read_friendlists,email";
    
    
        echo("<script> top.location.href='" . $dialog_url . "'</script>");
    }
    if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
        $token_url = "https://graph.facebook.com/oauth/access_token?"
           . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
           . "&client_secret=" . $app_secret . "&code=" . $code;
    
         $response = file_get_contents($token_url);
         $params = null;
         parse_str($response, $params);
         $longtoken=$params['access_token'];
    
    
    //save it to database   
    } 
    ?>
    
    <?
    require_once('scripts/facebook.php');
    // Pull access token from the file/db
    $access_token = "access token from file or db";
    $facebook->setAccessToken($access_token); // sets our access token as the access token when we call something using the SDK, which we are going to do now.
    
    $config = array('appId' => 'xxx','secret' => 'xxx');
    
    $params = array('scope'=>'user_likes,publish_actions,email,publish_stream,manage_pages');
    $facebook = new Facebook($config);
    $user = $facebook->getUser();
    if($facebook->getUser()) {
        try {
    
            $user_profile = $facebook->api('/me');
        } catch(FacebookApiException $e) {
            $login_url = $facebook->getLoginUrl($params);
            error_log($e->getType());
            error_log($e->getMessage());
        }   
    }
    else {
        $login_url = $facebook->getLoginUrl($params);
    }    
    
    $page_id = "xxxxxxxxxxxxx";
    $page_access_token = "";
    $result = $facebook->api("/me/accounts");
    foreach($result["data"] as $page) {
        if($page["id"] == $page_id) {
            $page_access_token = $page["access_token"];
            //echo '<br>';
            //echo "2. ".$page_access_token;
            break;
        }
    }
    
    $args = array(
        'access_token'  => $page_access_token,
        'message'       => stripslashes($image_caption).$animaged_gif,
        'name' => stripslashes($image_caption).$animaged_gif,
        'link' => "http://www.example.com/images.php?i=".$image_name,
        'picture' => "http://www.example.com/thumbnails/".$image_name.".png",
        'actions' => array(
                'name' => 'See Pic',
                 'link' => "http://www.example.com/images.php?i=".$image_name
        )
    );
    $post = $facebook->api("/$page_id/feed","post",$args);
    ?>
    
    
    
    确保不要在脚本中再次获取访问令牌(用户访问令牌/$access\u令牌)。

    另外,
    $args
    中的
    access\u token
    是页面访问令牌,应该由$page\u access\u token变量插入,而不是手动插入

    在作用域中进行
    脱机\u访问
    只会在权限对话框中显示他们可以随时访问他们的数据,而无需通知

    只要您在开始时访问fetch.php一次,并且每60天手动访问fetch.php一次,这项功能就会起作用


    让我知道。

    您可以放弃Facebook API的全部开销,采用这种极其简单的方法

    在页面设置()的“移动”部分中检查页面特定的电子邮件地址,然后在此处使用它:

    <?php
        mail("thatEmailAddress@facebook.com", "New Status Update!", "");
    ?>
    
    
    
    正文是空的,因为Facebook说“要更新您的状态,请在电子邮件主题行中填写,并将电子邮件正文留空。”

    您可以在此处查看。这是来自owloo.com的脚本库,用于通过“广告”仪表板从Facebook获取数据,还必须从Twitter和Instagram检索分析数据

    通过基本脚本,你可以从facebook上获取每个国家、城市、舞台、粉丝页面的趋势、兴趣、行为、人口统计、年龄、性别数量