繁体   English   中英

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

[英]Posting on user's facebook wall using php 2011

$ 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未打印。

您说您正在寻找更新的文档,是否检查了Facebook PHP-SDK常见问题解答

特别,

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

您创建一个应用程序实例后让你的$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;
    }

}

示例应用程序中所述,放入try / catch块以查看可用数据,具体取决于在进行API调用时用户是否已登录。

诸如

$cocacola = $facebook->api('/cocacola');

由于它是公开可用的,因此将始终有效。

以下是一些注意事项:

  1. 您正在使用新的PHP-SDK,所以不要使用req_perms使用scope
  2. 将您的/me/feed帖子通话放入 try
  3. 您不需要调用getUser()两次,用户ID已在$user
  4. 用户ID将已经在会话中,其密钥如下所示: fb_XXXXXXX_user_id其中XXXXXXX是您的应用ID
  5. 会话已经开始。

下面的代码肯定有效:即使您没有权限,它也会尝试获取它们

$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文档中看到,他们正在更改在用户墙上发布的方法,并且不鼓励使用发布流。 但目前

暂无
暂无

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

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