Facebook发布在PHP SDK页面上

Mis*_*iur 6 php facebook

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

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;

    }
Run Code Online (Sandbox Code Playgroud)

错误的部分是:

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

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

Fed*_*ras 16

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

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

        $this->loginUrl = $this->facebook->getLoginUrl(
              array(
                    'scope'         => 'manage_pages,offline_access,publish_stream',
                    'redirect_uri'  => $url,
                )
        );
    
    Run Code Online (Sandbox Code Playgroud)
  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();
    }
    
    Run Code Online (Sandbox Code Playgroud)