【问题标题】:Facebook post on page with PHP SDK使用 PHP SDK 在页面上发布 Facebook 帖子
【发布时间】:2010-10-24 17:11:39
【问题描述】:

我想在页面上发布 - 通过我的网站。 .我没有找到任何可以在文档中帮助我的东西。谷歌结果也没有给出答案。

function post_facebook($data=null){
        $result = "";
        require_once (ROOT. "/apps/configuration/models/ConfigurationItem.php");
        require_once (ROOT . "/components/facebook/facebook.php");

        $this->ConfigurationItem = new ConfigurationItem($this->getContext());

        $row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_login');
        $apiid=$row['value'];

        $row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_pass');
        $secret=$row['value'];

        $facebook = new Facebook(array(
          'appId'  => $apiid,
          'secret' => $secret,
          'cookie' => true,
        ));

        $session = $facebook->getSession();
        print_r($session);
        $me = null;
        if ($session) {
            try {
                $uid = $facebook->getUser();
                $me = $facebook->api('/me');
            } catch (FacebookApiException $e) {
                error_log($e);
            }
            $message=$data['facebook_text'];
            $attachment = array(
                'message' => $data['facebook_text'],
                'name' => $data['name'],
                'link' => $this->getLinkToLatestNews(),
                'description' => '',
            );

            if($data['thumb_file_tree_id'] !== NULL) $attachment = $_SERVER['HTTP_HOST']."media/file/image_by_id/".$data['thumb_file_tree_id']."/?w=400&h=500";

            try {
                $facebook->api('/162618213751448/feed/', 'post', $attachment);
                $result = "Facebook: Sent";
            } catch (FacebookApiException $e) {
                $result = "Facebook: Failed";
                error_log($e);
            }
        } else {
            $login_url = $facebook->getLoginUrl();
            header("Location: ".$login_url);
            exit;
        }

        return $result;

    }

错误的部分是:

$session = $facebook->getSession();
$me = null;
if ($session) {
    (...)
} else {
$login_url = $facebook->getLoginUrl();
    header("Location: ".$login_url);
    exit;
}

我想允许用户登录指定的 FB 帐户(此与页面),然后发送。应用程序设置只允许此帐户发布,所以应该没问题......但不是。当我注销 FB 帐户时,会话仍然存在,但返回异常。怎么了?

【问题讨论】:

    标签: php facebook


    【解决方案1】:

    如果你想在你是管理员的粉丝页面上发帖.. 下一步

    1. 您的应用程序至少需要拥有此权限:

          $this->loginUrl = $this->facebook->getLoginUrl(
                array(
                      'scope'         => 'manage_pages,offline_access,publish_stream',
                      'redirect_uri'  => $url,
                  )
          );
      
    2. 登录后:

      //get pages or applications where you are administrator
      $accounts = $this->facebook->api('/me/accounts');
      
      //page where i want to post
      $page_id = '227870047252297';
      
      foreach($accounts['data'] as $account)
      {
         if($account['id'] == $page_id)
         {
            $token = $account['access_token'];
         }
      }
      
      
      $attachment = array(
              'access_token' => $token,
              'message' => 'mensaje',
              'name' => 'titulo',
              'link' => 'http://...',
              'description' => 'xxxxx',
              'picture'=> 'http://...',
      );
      
      
      try{
      $res = $this->facebook->api('/'.$page_id.'/feed','POST',$attachment);
      
      } catch (Exception $e){
      
          echo $e->getMessage();
      }
      

    【讨论】:

      【解决方案2】:

      如果我理解正确,您想在登录时向通过您的网站连接到您的应用程序的用户墙发送消息?如果是这样,试试这个。重新编写脚本以防止不断发布,但这种模型理论上应该可以工作。

      if ($session) {
              try {
                  $uid = $facebook->getUser();
                  $me = $facebook->api('/me');
      

      // 将消息部分改为此处。

          $message = $facebook->api('/me/feed', 'post', array(
                                                                   'message'=> '<Message>', 
                                                                   'picture' => '<Picture URL>', 
                                                                   'link'=> '<URL>',
                                                                   'description'=>'Description', 
                                                                   'name'=> '<Name of Post>', 
                                                                   'privacy'=> '<ALL_FRIENDS (Default)>',
                                                                   'caption'=> '<Caption>',                                                           
      
                                                                   )
                                         );
      
                  } catch (FacebookApiException $e) {
                      error_log($e);
                  }
      

      因此,如果用户与您的应用程序连接并且会话有效,它将自动将帖子发送到他们的新闻提要(Wall)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-27
        • 1970-01-01
        • 2023-04-07
        • 2012-10-28
        • 1970-01-01
        相关资源
        最近更新 更多