2012-10-10 126 views
2

我创建了一个竞赛,提交的表单会:PHP API FACEBOOK - 用户发布到Facebook页面和个人墙

  1. 写在Facebook员工的墙壁和
  2. 评论写在墙上的评论我的页面

我对步骤1没有问题,但步骤2不起作用。我的代码如下:

connect.php

<?php 
//include the Facebook PHP SDK 
include_once 'facebook.php'; 

//instantiate the Facebook library with the APP ID and APP SECRET 
$facebook = new Facebook(array(
    'appId' => 'CRYPT FOR THIS FORUM', 
    'secret' => 'CRYPT FOR THIS FORUM', 
    'cookie' => true 
)); 

//Get the FB UID of the currently logged in user 
$user = $facebook->getUser(); 

//if the user has already allowed the application, you'll be able to get his/her FB UID 
if($user) { 
    //start the session if needed 
    if(session_id()) { 

    } else { 
     session_start(); 
    } 

    //do stuff when already logged in 

    //get the user's access token 
    $access_token = $facebook->getAccessToken(); 
    //check permissions list 
    $permissions_list = $facebook->api(
     '/me/permissions', 
     'GET', 
     array(
      'access_token' => $access_token 
     ) 
    ); 

    //check if the permissions we need have been allowed by the user 
    //if not then redirect them again to facebook's permissions page 
    $permissions_needed = array('publish_stream', 'read_stream'); 
    foreach($permissions_needed as $perm) { 
     if(!isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1) { 
      $login_url_params = array(
       'scope' => 'publish_stream,read_stream', 
       'fbconnect' => 1, 
       'display' => "page", 
       'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] 
      ); 
      $login_url = $facebook->getLoginUrl($login_url_params); 
      header("Location: {$login_url}"); 
      exit(); 
     } 
    } 

    //if the user has allowed all the permissions we need, 
    //get the information about the pages that he or she managers 
    //id pag sposiamo è 494659577226200 
    $accounts = $facebook->api(
     '/me/accounts', 
     'GET', 
     array(
      'access_token' => $access_token 
     ) 
    ); 

    //save the information inside the session 
    $_SESSION['access_token'] = $access_token; 
    $_SESSION['accounts'] = $accounts['data']; 
    //save the first page as the default active page 
    //$_SESSION['active'] = $accounts['data'][0];*/ 

    //redirect to manage.php 
    header('Location: manage.php'); 
} else { 
    //if not, let's redirect to the ALLOW page so we can get access 
    //Create a login URL using the Facebook library's getLoginUrl() method 
    $login_url_params = array(
     'scope' => 'publish_stream,read_stream', 
     'fbconnect' => 1, 
     'display' => "page", 
     'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] 
    ); 
    $login_url = $facebook->getLoginUrl($login_url_params); 

    //redirect to the login URL on facebook 
    header("Location: {$login_url}"); 
    exit(); 
} 

?> 

newpost.php

<?php 
//include the Facebook PHP SDK 
include_once 'facebook.php'; 

//start the session if necessary 
if(session_id()) { 

} else { 
    session_start(); 
} 

//instantiate the Facebook library with the APP ID and APP SECRET 
$facebook = new Facebook(array(
    'appId' => 'CRYPT', 
    'secret' => 'CRYPT', 
    'cookie' => true 
)); 

//get the info from the form 
$parameters = array(
    'message' => $_POST['message'], 
    'picture' => $_POST['picture'], 
    'link' => $_POST['link'], 
    'name' => $_POST['name'], 
    'caption' => $_POST['caption'], 
    'description' => $_POST['description'] 
); 

//add the access token to it 
$parameters['access_token'] = $_SESSION['active']['access_token']; 

//build and call our Graph API request 
$newpost = $facebook->api(
    '/494659577226200/feed', 
    '/me/feed', 
    'POST', 
    $parameters 
); 

//redirect back to the manage page 
header('Location: manage.php'); 
exit(); 

494659577226200 = FBPAGEID

问题是 '/ 494659577226200 /饲料' 和错误校验码200 ...

+1

'$ newpost = $ facebook-> API墙后( '/ 494659577226200 /饲料', '/我/饲料', 'POST', $参数 );'应该是$ newpost = $ facebook-> API( '/ 494659577226200 /进料', 'POST', $参数 ); – phwd

+0

无事可做......我认为有一个页面的身份验证问题...令牌问题?使用Facebook API是一个噩梦......糟糕的文档! –

回答

0

你需要问你的用户给你的应用程序manage_pages允许发布到他们代表他们管理的页面。查看他们的权限doc here,请参阅Page Permissions部分。

从文档报价: manage_pages

使您的应用程序检索页面和用户下辖应用access_tokens。访问令牌可以通过Graph API调用//帐户来查询。此权限仅与Graph API兼容,不兼容弃用的REST API。 请参阅此处,了解60天后不会过期的长期页面访问令牌。

一旦你得到这个许可,您可以让使用page access token