2014-04-13 96 views
2

我使用的是Facebook的PHP SDK,我试图发布在由登录用户管理/管理的页面上。我已授予publish_stream和manage_pages权限。我希望帖子应该看起来像是由页面而不是管理员或登录用户创建的。我尝试了几次帮助,但没有人工作。这是我的我现有的代码部分:Facebook的PHP SDK:我怎样才能在页面上发布代表页面

require './php-fb-sdk/facebook.php'; 

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array('appId' => 'xxxx', 'secret' => 'zzzz')); 

// Get User ID 
$user = $facebook -> getUser(); 

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

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; 
    } 
} 

$params = array("scope" => array("manage_pages", "publish_stream")); 
// Login or logout url will be needed depending on current user state. 
if ($user) { 
    // Fetch the viewer's basic information 
    $basic = $facebook -> api('/me'); 

    $permissions = $facebook -> api("/me/permissions"); 
    if (array_key_exists('manage_pages', $permissions['data'][0]) && array_key_exists('publish_stream', $permissions['data'][0])) { 

     $admin_pages = $facebook -> api(array('method' => 'fql.query', 'query' => "SELECT page_id, type from page_admin WHERE uid=me() and type!='APPLICATION'")); 
     if (count($admin_pages) > 0) { 

      $post_info = $facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time())); 
      echo '<hr>The post info is: ' . print_r($post_info, true) . '<hr>'; 
     } else { 
      echo '<hr> You are not admin of any fb fan page<hr>'; 

     } 
     //print_r($admin_pages); 

    } else { 
     // We don't have the permission 
     // Alert the user or ask for the permission! 
     header("Location: " . $facebook -> getLoginUrl(array("scope" => "manage_pages,publish_stream"))); 
    } 
    $logoutUrl = $facebook -> getLogoutUrl(); 
} else { 
    //$statusUrl = $facebook->getLoginStatusUrl(); 
    $loginUrl = $facebook -> getLoginUrl($params); 
    $statusUrl = $loginUrl; 
} 

使用上面的代码,我可以张贴在页面上,但看起来它是由用户制作。

但是,如果我使用Facebook的JS SDK,那么我看到的帖子看起来像它是由页面。

var data= 
      { 
       caption: 'My Caption', 
       message: 'My Message' 
      } 

      FB.api('/' + pageId + '/feed', 'POST', data, onPostToWallCompleted); 
      } 

任何帮助或建议将不胜感激。

回答

1

要代表页面发布页面,您需要使用页面访问令牌。您目前的电话:

$facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time())); 

正在使用默认(用户)访问令牌。

让页面访问令牌,令发布后在此之前调用:

\GET /{page-id}?fields=access_token 

这将让你在结果的页面访问令牌,然后只需用它来使你的发布提要通话,就像这 -

$facebook -> api(
    '/' . $admin_pages[0]["page_id"] . '/feed', 
    'post', 
    array(
    "caption" => "From web", 
    "message" => "This is from my web at: " . time(), 
    "access_token" => '{page_access_token}' 
) 
); 

(如果需要的话,你也可以得到你的页面的到期从来没有道理,在这里查询如何:What are the Steps to getting a Long Lasting Token For Posting To a Facebook Fan Page from a Server

+0

非常感谢兄弟。有效! –

相关问题