获取Facebook用户的许可并发布到他们的墙上

时间:2011-11-03 11:01:35

标签: php facebook api permissions

Post to users wall upon facebook app submission(我的旧问题)之后,我想出了以下代码,但它似乎没有起作用?我认为最好打开一个新问题,因为这是一个新问题。

我做错了什么?此外,这段代码应该放在哪里?

<?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
?>

2 个答案:

答案 0 :(得分:1)

我只会发布我正在使用的代码。希望它有所帮助

  

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/')
                ));
        }

答案 1 :(得分:0)

您需要一个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",
);

您可以访问令牌here

相关问题