Facebook在PHP页面上发布 [英] Facebook post on page with PHP SDK

查看:85
本文介绍了Facebook在PHP页面上发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在页面上发布 - 通过我的网站。 。我没有找到任何可以帮助我的文档。也没有一个谷歌的结果给了mi答案。

  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);
退出;
}

return $ result;

}

错误的部分是:

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

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

解决方案

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


  1. 您的应用程序需要最小权限:

      $ this-> loginUrl = $ this-> facebook-> getLoginUrl(
    array(
    'scope'=>'manage_pages,offline_access,publish_stream',
    'redirect_uri'=> $ url,

    );


  2. 登录后:



    $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

    //我要发布的页面
    $ 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:// ...',
    );


    尝试{
    $ res = $ this-> facebook-> api('/'.$ page_id。'/ feed','POST',$ attachment );

    } catch(异常$ e){

    echo $ e-> getMessage();
    }



I'd like to post on page - throught my site. . I didn't find anything that could help me in documentation. Also none of google results gave mi answer.

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;

    }

The wrong part is:

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

I want to allow user to login on specified FB account (this with page), and then send. App settings allow only this account to post, so it should be ok... But isn't. When I log out FB account, session still exists, but returns exception. What's wrong?

解决方案

if you want to post on a fan page where you are adminstrator.. do the next

  1. Your application needs to have this permission minimum:

        $this->loginUrl = $this->facebook->getLoginUrl(
              array(
                    'scope'         => 'manage_pages,offline_access,publish_stream',
                    'redirect_uri'  => $url,
                )
        );
    

  2. After you are logged into:

    //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();
    }
    

这篇关于Facebook在PHP页面上发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆