简体   繁体   中英

Facebook post on page with PHP SDK

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?

if you want to post on a fan page where you are adminstrator.. do the next

  1. Your application needs to have this permission minimum:

      $this->loginUrl = $this->facebook->getLoginUrl( array( 'scope' => 'manage_pages,offline_access,publish_stream', 'redirect_uri' => $url, ) ); 
  2. After you are logged into:

     //get pages or applications where you are administrator $accounts = $this->facebook->api('/me/accounts'); //page where i want to post $page_id = '227870047252297'; foreach($accounts['data'] as $account) { if($account['id'] == $page_id) { $token = $account['access_token']; } } $attachment = array( 'access_token' => $token, 'message' => 'mensaje', 'name' => 'titulo', 'link' => 'http://...', 'description' => 'xxxxx', 'picture'=> 'http://...', ); try{ $res = $this->facebook->api('/'.$page_id.'/feed','POST',$attachment); } catch (Exception $e){ echo $e->getMessage(); } 

If I understand this correctly, you want to send a message to the user's wall who's connected to your application via your website the moment they log in? If so, try this. Rework the script to prevent constant posting, but this model, in theory, should work.

if ($session) {
        try {
            $uid = $facebook->getUser();
            $me = $facebook->api('/me');

// Place messaging portion here instead.

    $message = $facebook->api('/me/feed', 'post', array(
                                                             'message'=> '<Message>', 
                                                             'picture' => '<Picture URL>', 
                                                             'link'=> '<URL>',
                                                             'description'=>'Description', 
                                                             'name'=> '<Name of Post>', 
                                                             'privacy'=> '<ALL_FRIENDS (Default)>',
                                                             'caption'=> '<Caption>',                                                           

                                                             )
                                   );

            } catch (FacebookApiException $e) {
                error_log($e);
            }

So if the user is connected with your application and the session is valid, it will automatically send the post to their news feed (Wall).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM