获得facebook用户的许可并在他们的墙上发布


Grabbing permission from facebook user and posting to their wall

进一步发布到用户墙上的facebook应用程序提交(我的老问题),我已经提出了以下代码,但它似乎不工作?我想最好开一个新问题,因为这是一个新问题。

我做错了什么?同样,这些代码应该放在哪里?

<?php
$session = $facebook->getSession();
//Is user logged in and has allowed this app to access its data
if (!$session) {
    $loginUrl = $facebook->getLoginUrl(array(
    'canvas' => 1,
    'fbconnect' => 0,
    'next' => 'enter.php',
    'cancel_url' => 'index.php',
    ));    
//    use the $loginUrl created on the enter button to request permission;
}
$user_id = $facebook->getUser();

//post to wall
    $attachment = array('message' => '<message>',
                    'name' => '<name here>',
                    'caption' => '<caption here>',
                    'link' => '<link to app>',
                    'description' => '<enter description >',
                    'picture' => '<enter image url>',
                    'actions' => array(array('name' => '<enter action label>', 
                                      'link' => '<enter action url>')
                    );
    $permissions = $facebook->api("/me/permissions");
    if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
// Permission is granted!
// Do the related task
try {
$post_id = $facebook->api('/me/feed', 'post', $attachment);
    } catch (CurlException $e) {
//timeout so try to resend
$post_id = $facebook->api('/me/feed', 'post', $attachment);
    } 
    catch (FacebookApiException $e) {
error_log($e);
    }   
    } else {
// We don't have the permission
// Alert the user or ask for the permission!
    }
// store the post id in-case you need to delete later
?>

我将发布我正在使用的代码。希望能有所帮助

fbClass.php

    public function __construct() {
    // Naredimo instanco
    $facebook = new Facebook(array(
                'appId' => $this->fbid,
                'secret' => $this->fbsecret,
                'cookie' => true,
            ));
    $this->facebook = $facebook;
}
function authUser($facebook) {
    $user = $facebook->getUser();
    if ($user) {
        try {
            // Proceed knowing you have a logged in user who's authenticated.
            $user_profile = $facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }
    }
    // Login or logout url will be needed depending on current user state.
    if (!($user)) {
        $loginUrl = $facebook->getLoginUrl(array(
                    'scope' => 'user_about_me, user_birthday, email, publish_stream',
                    'redirect_uri' => 'http://apps.facebook.com/myappname/',
                ));
        echo("<script> top.location.href='" . $loginUrl . "'</script>");
    } else {
        return true;
    }
}

process.php

$facebook = $fbClass->facebook;
$fbAuth = $fbClass->authUser($facebook);
if ($fbAuth) {
        $res = $facebook->api('/me/feed/', 'post', array(
                    'message' => MESSAGE,
                    'name' => NAME,
                    'caption' => '',
                    'description' => DESC,
                    'picture' => PIC,
                    'link' => 'http://www.facebook.com/myapp/',
                    'actions' => array('name' => 'Test', 'link' => 'http://apps.facebook.com/myapp/')
                ));
        }

需要一个Facebook访问令牌才能使此代码工作。在My Access token here所在的位置添加令牌:

$attachment = array(
'access_token' => 'My Access token here',
'message'      => '',
'name'         => 'My Wall Post Header/Title Here',
'caption'      => 'Small caption here',
'link'         => 'http://www.mywebsite.org',
'description'  => 'Wall Post Details Here',
'picture'      => "http://www.mywebsite.org/images/logo.gif",
);

您可以在这里获得访问令牌