tags:

views:

40

answers:

1

Hi there. 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!

+1  A: 

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.

Take a look at: http://developers.facebook.com/docs/api#impersonation

Here's an example. You can search for '/me/accounts' to see how it's handled: http://codadevelopment.net/example.txt

njorden
The key to solve this was getting access token - I missed that part earlier. I owe you!
Misiur