views:

21

answers:

0

Hi there.

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?