在Facebook页面上发布帖子作为页面管理员

时间:2016-04-24 11:52:52

标签: php facebook codeigniter facebook-graph-api

我有CodeIigniter 3项目,我有一个博客帖子功能,我想在Facebook页面添加一个自动发布。我已经使用CodeIgniter 3项目配置了HyBridAuth。此外,我已经验证了我的Facebook并使用访问令牌存储了Facebook会话的数据。

现在,我正在尝试发布到我的Facebook页面。我创建了以下方法。

public function facebook_post() {
    if ($this->ion_auth->logged_in()) {
        $this->data['userInformation'] = $this->ion_auth->user()->row();
    }
    $this->data['userid'] = $this->data['userInformation']->id;
    $this->data["facebook_status"] = $this->admin_model->getStoredHybridSession($this->data['userid'], "Facebook");

    if(!empty($this->data['facebook_status'][0])) {
        if($this->data["facebook_status"] != "0") {
            $this->data["facebook_profile"] = $this->get_profile("Facebook");
            $access_token = explode('&', $this->data['facebook_profile']->coverInfoURL);
            $a_t = explode('=', $access_token[1]);

            $params = array(
                "access_token" => $a_t[1],
                "message" => "Here is a blog post about auto posting on Facebook using PHP",
                "link" => "http://facebook.com",
                "picture" => "https://i.telegraph.co.uk/multimedia/archive/03474/Facebook_3474124b.jpg",
                "name" => "Auto post functionality test",
                "caption" => "https://facebook.com",
                "description" => "Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation."
            );
            $facebook = $this->hybridauthlib->authenticate("Facebook");
            $ret = $facebook->api()->api('/1388840631445245/feed', 'POST', $params);
            owndebugger('Successfully posted to Facebook');
        }
    } else {
        $this->data["facebook_profile"] = 0;
    }
}

此代码正常运行。但是,我的帖子张贴到页面作为访客帖子。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我已成功完成。 :)

要在页面上发布,需要考虑以下信息。

1)范围:publish_pages, publish_actions, manage_pages

2)您必须将长期存在的access_token存储在页面中定期发布。

3)然后,您必须使用post方法中的现有存储会话再次登录,如下所示:

$this->data["facebook_profile"] = $this->get_user_pages("Facebook");
    $params = array(
    "access_token" => $this->data['facebook_profile'][0]['access_token'],
    "message" => $this->input->post('posttitle'),
    "link" => base_url() . "news/" . $this->results,
    "picture" => base_url() . "uploads/posts/".(isset($profilephoto['file_name']) ? $profilephoto['file_name'] : $pp),
    "name" => "Stack Overflow",
     "caption" => "http://stackoverflow.com",
     "description" => $this->input->post('postcontent')
    );
// post on a single page of my pages
    $facebook = $this->hybridauthlib->authenticate("Facebook");
    $facebook->api()->api("/221320344636/feed", 'POST', $params );

4)$this->get_user_pages("Facebook")。在get_user_pages检索您的所有网页access_token,然后您可以选择access_token您要在该网页上发布的一个网页。

我要特别感谢@luschn的帮助。 :)