简体   繁体   English

取得Facebook用户的许可并张贴到他们的墙上

[英]Grabbing permission from facebook user and posting to their wall

Further to Post to users wall upon facebook app submission (my old question), I have came up with the following code however it doesn't seem to be working?? 除了在Facebook应用提交后向用户发布墙 (我的老问题)之外,我想出了以下代码,但似乎不起作用? I thought best to open a new question as it is a new question. 我认为最好打开一个新问题,因为这是一个新问题。

What am I doing wrong? 我究竟做错了什么? Also, where should this code go? 另外,此代码应放在哪里?

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

I'll just post the code I'm using that works. 我只发布我正在使用的代码即可。 Hope it helps 希望能帮助到你

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

You need a Facebook access token for this code to work. 您需要一个Facebook访问令牌才能使此代码正常工作。 Add your token where My Access token here is in the following code: 将您的令牌添加到以下代码中的“ 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",
);

You can get access tokens here . 您可以在此处获取访问令牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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