Facebook api PHP:发布给其他用户页面提要不起作用

时间:2013-10-02 08:35:48

标签: php facebook

我一直在尝试在网页Feed上发布消息和图片。我一直收到错误消息

  

(#1)创建共享时出错。

只有在其他人试图发帖时才会发生这种情况。如果我,页面的所有者试图发布它是成功的。

所以我的问题是:是否可以发布给其他用户的网页Feed?

我已经对此做了大量研究,但我似乎无法找到合理的解决方案。

以下是我用来发布消息的代码:

<?php

class Photo_contest_helper extends Facebook
{
    private $_key = '***********';
    private $_secret = '***************';

    private $_page_id = '387228561388254';

    public $fb_login_url;
    public $fb_logout_url;

    public $image_id;

    public function init()
    {
        $this->connect();

        $image_id = $this->save_image();

        if( !!$image_id ) {
            $this->image_id = $image_id;
            $this->push_to_facebook( $image_id );
        }
    }

    public function save_image()
    {
        $image_id = NULL;

        if ( !!$_POST[ "image" ] || !!$_FILES ) {
            $image_id = Image_helper::save_one( $_POST[ "image" ] );
        }

        return $image_id;
    }

    public function push_to_facebook( $image_id )
    {
        $access_token = $this->get_page_access_token();

        $this->publish_photo( $access_token );
    }

    public function connect()
    {
        parent::__Construct( array( 'appId' => $this->_key, 'secret' => $this->_secret ) );

        $user = $this->getUser();

        if( !$user ) {
            $this->fb_login_url = $this->getLoginUrl( array( 'scope' => 'manage_pages,publish_actions,publish_stream,status_update' ) );
        }
        else {
            $this->fb_logout_url = $this->getLogoutUrl( array( 'next' => 'http://stormtest.co.uk/' . DIRECTORY . "home/logout" ) );
        }
    }

    public function get_page_access_token()
    {
        $accounts = $this->api( '/me/accounts', 'get' );
        $access_token = NULL;

        foreach ( $accounts[ 'data' ] as $account ) {
            if ( $account[ 'id' ] == $this->_page_id ) {
                $access_token = $account[ 'access_token' ];
            }
        }

        return $access_token;
    }

    public function publish_photo( $access_token = "" )
    {

        $params = array( 'message' => 'Competition entry',
                         'link' => 'http://stormtest.co.uk/' . DIRECTORY . '_admin/assets/uploads/images/' . $this->get_image(),
                         'caption' => 'This is my competition entry',
                         'picture' => 'http://stormtest.co.uk/' . DIRECTORY . '_admin/assets/uploads/images/' . $this->get_image() );

        if( !!$access_token ) {
            $params[ 'access_token' ] = $access_token;
        }


        try {
            $this->api( '/' . $this->_page_id . '/feed', 'post', $params );
        }
        catch( FacebookApiException $e ) {
            die( print_r( $e ) );
        }
    }

    public function get_image()
    {
        $image_model = new Image_model();
        $image_model->find( $this->image_id );

        return $image_model->attributes[ 'imgname' ];
    } 
}

?>

提前致谢。

1 个答案:

答案 0 :(得分:0)

您应该使用JavaScript SDK与页面共享内容。通过API发布到他人的墙壁已被禁用:

FB.ui( {
    method: 'feed',
    to: '387228561388254',
    name: "Facebook API: Tracking Shares using the JavaScript SDK",
    link: "https://www.webniraj.com/2013/05/11/facebook-api-tracking-shares-using-the-javascript-sdk/",
    picture: "https://stackexchange.com/users/flair/557969.png",
    caption: "Tracking Facebook Shares on your website or application is a useful way of seeing how popular your articles are with your readers. In order to tracking Shares, you must used the Facebook JavaScript SDK."
}, callback );

使用上面代码中的to字段设置page_id,并相应地更改其他属性(但不要更改method)。