Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook Graph API PHP SDK posting on page as page

Tags:

php

facebook

api

It's final try with PHP, if it fails, I'll try with JS. So my goal is to post on FB page as "Page name" through PHP: this is what I want to get

View 1

But all I get is shown pic below. Also, it's visible ONLY to this profile (not to friends/ppl who like/etc.).

View 2

This is my current code

function post_facebook($data=null, $redir = 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']; <= Correct apiid

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

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

        $session = $facebook->getSession();
        $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' => '',
            );

            try {
                $facebook->api('/PAGE ID/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;
        }

        echo $result;
        exit;
        //return $result;

    }

What I'm doing wrong? I couldn't find anything in API documentation/top google results, only for JS. Thanks for help!

like image 763
Misiur Avatar asked Oct 29 '10 19:10

Misiur


1 Answers

You'll need to make sure you're requesting the 'manage_pages' permission for the user. Once you've got that you can do $facebook->api('/me/accounts') and you'll receive a token back (along with the page info) that you can use to post on the page as the page.

like image 73
njorden Avatar answered Sep 23 '22 07:09

njorden