2011-11-03 32 views
0

Post to users wall upon facebook app submission(我的老問題),我想出了下面的代碼,但它似乎並沒有工作?我認爲最好能夠提出一個新問題,因爲這是一個新問題。從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 
?> 

回答

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

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

嗨,彼得,我試過這段代碼,但沒有喜悅。你以什麼方式設置它?你如何調用你的代碼?另外,你是否需要訪問令牌? –

+0

那麼,你必須使用它的類。 'fbClass.php'以'class fbClass {0}開頭private 0 $ fbid =「my app id」; private $ fbsecret =「secret」;' 然後我把它包含在'process.php'中,這個: 'require'classes/facebook-sdk/src/facebook.php'; require'classes/fbClass.php';' 我創建了一個新實例:'$ fbClass = new fbClass();'然後調用函數: '$ facebook = $ fbClass-> facebook; $ fbAuth = $ fbClass-> authUser($ facebook);' 就是這樣。 – Wowca

+0

你可能會發布你的完整process.php和fbClass.php頁面嗎?所以我可以理解我將如何使用我的代碼實現它?另外,我應該在哪裏調用process.php頁面?我有三個主要頁面,index.php(進入app的按鈕),enter.php(表單等等),thankyou.php(謝謝你) –

相關問題