简体   繁体   中英

Facebook page - post to users wall(after the user has liked the page)

I hope it's not that complicated but since Facebook has changed its API a lot(concerning permissions and so on) I find many outdated info which doesn't really help me. The situation is:

  1. I got a Facebook page with Page Tab
  2. I got a Facebook-App which is associated with that page(via the tab).
  3. My own WebApp posts via Facebook PHP-SDK to the wall of the page and each user sees the news in the stream.

Question: How can I post to the users timeline via that app?

My code so far(which posts to the wall of the page)

require 'facebook.php';
$facebook = new Facebook(array(
    'appId'  => 'xxxxxxxxxxxxxx',
    'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$loginUrl = $facebook->getLoginUrl();
$user = $facebook->getUser();

$page_id = 'xxxxxxxxxxxxxxxxxxxx';
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) ) {
    $args = array(
        'access_token'  => $page_info['access_token'],
        'message'       => "Neuer Test!"
    );
    $post_id = $facebook->api("/$page_id/feed","post",$args);
} else {
    $permissions = $facebook->api("/me/permissions");
    if( !array_key_exists('publish_stream', $permissions['data'][0]) || 
        !array_key_exists('manage_pages', $permissions['data'][0])) {
        header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream, manage_pages")) );
    }

}

This is working fine, but the message is not available in the timeline of the users who liked the page, only in their own stream. Is it possible to publish to their timeline?

dont know if you still need help. but just in case, this code works for me. It assumes you have a logged in user. and it posts to the user's timeline. this code doesn't get the permission which i believe, you need to work on since Facebook has Platform TOS. try this one:

     require_once('libs/facebook.php');

    // init new facebook class instance with app info (taken from the DB)
    $facebook = new Facebook(array(
        'appId' => 'your app id here',
        'secret' => 'your app secret here'
    ));



    $fb_user_id = $facebook->getUser();
    $location = "". $facebook->getLoginUrl(array('scope' => 'publish_stream'));

    if ($fb_user_id) {
        try {
            $fb_user_profile = $facebook->api('/me');   

        } catch (FacebookApiException $e) {
            $fb_user_id = NULL;

            print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';
            die();
        }
    } else {

        print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';
        die();
    }


    $ret_obj = $facebook->api('/me/feed', 'POST',
                            array(
                            'link' => 'www.google.com',
                            'message' => 'Google me!'
                                             ));

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