简体   繁体   English

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

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

Nothing gets posted to the wall, execution gets out of try after $result = $facebook->api('/me/feed/','post',$attachment); $ result = $ facebook-> api('/ me / feed /','post',$ attachment)之后,一切都不会搁在墙上,执行会变得毫无尝试。 statement, any idea whats broken. 声明,任何想法都坏了。

$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>');
}

Test 1 is printed but not Test 2. 测试1已打印,但测试2未打印。

You said you were looking for updated documentation, did you check Facebook PHP-SDK FAQ ? 您说您正在寻找更新的文档,是否检查了Facebook PHP-SDK常见问题解答

Specifically, 特别,

  • How to authorize and have any of the following permissions? 如何授权并拥有以下任何权限?
  • How to post on a wall? 如何在墙上张贴?

After you create an Application instance get your $user first 您创建一个应用程序实例后让你的$user 第一

$user = $facebook->getUser();

From here, following the instructions from "How to authorize and have any of the following permissions?" 从此处开始,按照“如何授权并具有以下任何权限?”中的说明进行操作。 using the scope 使用范围

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

Check the user state to see which login/logout method is required passing the publish_stream permission 检查用户状态,以查看通过publish_stream权限所需的登录/注销方法

if ($user) {
        $logoutUrl = $facebook->getLogoutUrl();
} else {
        $loginUrl = $facebook->getLoginUrl($par);
}

Then place the attachment as explained in "How to post on a wall?" 然后按照“如何在墙上张贴?”中的说明放置附件。

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

}

As explained in the example app , put in a try/catch block to see what data is available depending on whether the user is logged in or not when making API calls. 示例应用程序中所述,放入try / catch块以查看可用数据,具体取决于在进行API调用时用户是否已登录。

A call such as 诸如

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

Will always work since it is publicly available. 由于它是公开可用的,因此将始终有效。

Here are a couple of notes: 以下是一些注意事项:

  1. You are using the new PHP-SDK so don't use req_perms use scope instead 您正在使用新的PHP-SDK,所以不要使用req_perms使用scope
  2. Put your /me/feed post call inside the try 将您的/me/feed帖子通话放入 try
  3. You don't need to call getUser() twice, the user id is already in the $user 您不需要调用getUser()两次,用户ID已在$user
  4. The user id will be already in the session, with key that looks like: fb_XXXXXXX_user_id where XXXXXXX is your app id 用户ID将已经在会话中,其密钥如下所示: fb_XXXXXXX_user_id其中XXXXXXX是您的应用ID
  5. session already started.. 会话已经开始。

below code surely works: even if you dont have permission it will try to get them 下面的代码肯定有效:即使您没有权限,它也会尝试获取它们

$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")) );
            }

*Warning *警告

as of sept 5 2011 this is working, but i saw on fb documentation they are changing method to poste on users wall and are discouraging use of publish stream. 截至2011年9月5日,此功能已开始运行,但我在fb文档中看到,他们正在更改在用户墙上发布的方法,并且不鼓励使用发布流。 but its working for now 但目前

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

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