使用php 2011在用户的Facebook墙上发布

时间:2011-08-27 22:53:35

标签: php facebook facebook-graph-api

没有任何内容发布到墙上,执行结果是在$ result = $ facebook-> api('/ me / feed /','post',$ attachment)之后尝试;声明,任何想法都破了。

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));

// Get User ID


$user = $facebook->getUser();
if ($user) {
  try {
    // Get the user profile data you have permission to view
    $user_profile = $facebook->api('/me');
    $uid = $facebook->getUser();


      $url = $facebook->getLoginUrl(array(
    'canvas' => 1,
    'fbconnect' => 0,
    'req_perms' => 'email,publish_stream,status_update,user_birthday,user_location,user_work_history'));


    $attachment = array
 (
 'access_token'=>$facebook->getAccessToken(),
 'message' => 'I had a question: should I write a PHP facebook app that actually worked?',
 'name' => 'I Asked Bert',
 'caption' => 'Bert replied:',
 'link' => 'http://apps.facebook.com/askbert/',
 'description' => 'NO',
 'picture' => 'http://www.facebookanswers.co.uk/img/misc/question.jpg'
 );
 echo "Test 1"; 

$result = $facebook->api('/me/feed/','post',$attachment);

    echo "Test 2"; 
    $_SESSION['userID'] = $uid;


  } catch (FacebookApiException $e) {
    $user = null;
  }
} else {
  die('Somethign Strange just happened <script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}

打印测试1但不打印测试2.

3 个答案:

答案 0 :(得分:5)

您说您正在寻找更新的文档,是否检查了Facebook PHP-SDK FAQ

具体地,

  • 如何授权并拥有以下任何权限?
  • 如何贴在墙上?

创建应用实例后,首先获取$user

$user = $facebook->getUser();

从这里开始,按照“如何授权并拥有以下任何权限?”中的说明进行操作。使用范围

$par = array();
$par['scope'] = "publish_stream";

检查用户状态以查看通过publish_stream权限

所需的登录/注销方法
if ($user) {
        $logoutUrl = $facebook->getLogoutUrl();
} else {
        $loginUrl = $facebook->getLoginUrl($par);
}

然后按照“如何在墙上张贴?”中的说明放置附件。

if ($user) {
$attachment = array('message' => 'this is my message',
    'name' => 'This is my demo Facebook application!',
    'caption' => "Caption of the Post",
    'link' => 'http://mylink.com/ ',
    'description' => 'this is a description',
    'picture' => 'http://mysite.com/pic.gif ',
    'actions' => array(array('name' => 'Get Search',
    'link' => 'http://www.google.com/ '))
    );

    try {
    // Proceed knowing you have a user who is logged in and authenticated
    $result = $facebook->api('/me/feed/','post',$attachment);
    } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
    }

}

example app中所述,根据用户是否在登录时进行API调用,输入try / catch块以查看可用的数据。

诸如

之类的电话
$cocacola = $facebook->api('/cocacola');

因为它是公开的,所以一直有效。

答案 1 :(得分:1)

以下是几点说明:

  1. 您正在使用新的PHP-SDK,因此请勿使用req_perms使用scope代替
  2. 将您的/me/feed帖子置于 try
  3. 您无需拨打getUser()两次,用户ID已在$user
  4. 用户ID已经在会话中,其密钥类似于:fb_XXXXXXX_user_id其中XXXXXXX是您的应用ID
  5. 会话已经开始..

答案 2 :(得分:0)

下面的代码确实有效:即使你没有获得许可也会尝试获取它们

$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user = $facebook->getUser();
$user_profile = $facebook->api('/me');


if( array_key_exists('publish_stream', $permissions['data'][0]) ) {
                // Permission is granted!
                // Do the related task
                //$post_id = $facebook->api('/me/feed', 'post', array('message'=>'Hello World!'));
                  $post_id = $facebook->api('/me/feed', 'post', $attachment);

            } else {
                // We don't have the permission
                // Alert the user or ask for the permission!
                echo "Click Below to Enter!";
                header( "Location: " . $facebook->getLoginUrl(array("scope" => "publish_stream")) );
            }
  

*警告

截至2011年9月5日,这是有效的,但我在fb文档中看到他们正在改变在用户墙上发布的方法,并且不鼓励使用发布流。但它现在正在工作