【问题标题】:Post to page via Facebook API通过 Facebook API 发布到页面
【发布时间】:2014-04-12 03:47:09
【问题描述】:

我是 FB 应用程序和 FB 页面的管理员(但此页面在单独的帐户上)。我如何使用 FB API 和 PHP 通过这个 FB 应用程序在这个页面的墙上发布一些东西(以便能够使用 CRON 来做到这一点)?那可能吗?提前感谢您的回答!

【问题讨论】:

    标签: php facebook facebook-graph-api


    【解决方案1】:

    是的,这是可能的。

    首先,使用页面访问令牌来代表其在页面上发帖。

    从您的应用程序中获取一个普通令牌(可以通过从右上角的下拉菜单中选择您的应用程序直接使用Graph API Explorer)并获得权限:manage_pages,然后按照我在此处提到的步骤操作:@987654322 @ - 这将为您提供一个永不过期的页面访问令牌。

    将其保存在某处并在发布时与您的 cron-job 一起使用。发布代码-

    $url = 'https://graph.facebook.com/{page-id}/feed';
    $attachment =  array(
        'access_token'  => $page_access_token,
        'message'  => '{your-message}'
    );
    $result = PostUsingCurl($url, $attachment);
    $result = json_decode($result, TRUE);
    if( isset($result['error']) ) {
       echo "Error: ".$result['error']['message']."<br/>";
    }
    else{
      echo "Feed posted successfully!<br/>";
    }
    
    function PostUsingCurl($url, $attachment)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close ($ch);
    
        return $result;
    }
    

    【讨论】:

      【解决方案2】:

      我个人使用这个。尽管您需要已经生成了 access_token。如果您不这样做,您可以使用Facebook's Graph Explorer tool 为您的帐户授予适当的权限。

      $attachment =  array(
      "access_token" => $fb_token,
      "link" => "$postLink",
      "name" => "$postName",
      "description" => "$postDescription",
      "message" => "$postMessage",
      "fb:explicitly_shared" => true
      );
      
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$fb_page_id.'/feed');
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output 
      $result = curl_exec($ch);
      curl_close ($ch);
      

      希望对您有所帮助!

      【讨论】:

        【解决方案3】:

        将 API 添加到您的页面

        <script id="facebook-jssdk" src="//connect.facebook.net/en_US/all.js#xfbml=1"></script>`
        

        点击函数调用fb页面

        $('#facebook').click(function(){
          FB.init({
            appId: 12345, // your app ID
            status: true,
            cookie: true
          });
          FB.ui({
            method: 'feed',
            name: "post name",
            link: "http://postlink.com,
            //picture: "http:/imageurl.com,
            description: "this is the body of the text"
          });
        
        })  
        

        【讨论】:

        • 感谢您的回答,但这并不是我想要的。您的代码有效,但它会将消息放在我的个人帐户的墙上,而不是页面的墙上。另外我忘了提到我想通过 PHP 来做基于服务器的操作。我已经相应地更新了我的问题。
        猜你喜欢
        • 1970-01-01
        • 2012-11-08
        • 2010-10-31
        • 1970-01-01
        • 2014-08-24
        • 1970-01-01
        • 1970-01-01
        • 2018-02-07
        • 2018-08-27
        相关资源
        最近更新 更多