以Facebook的一系列用户为例 - 发布到朋友的墙上

时间:2012-02-01 14:07:36

标签: javascript php facebook facebook-graph-api

我有一系列Facebook用户,使用自定义Facebook Multi-Selector输出。

输出是所选用户的列表。

如何获取这些ID号码,然后使用Facebook API将消息发布到他们的墙上?

1 个答案:

答案 0 :(得分:1)

下面是我之前使用的PHP代码片段

$attachment =  array(
  'from' => $_SESSION['username'],
  'access_token' => $access_token,
  'message' => $message,
  'name' => $title,
  'link' => $url,
  'description' => $description,
  'caption' => $caption,
  'picture' => $img,
  'privacy' => json_encode(array('value' => 'FRIENDS_OF_FRIENDS'))
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$userid.'/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output 
$result = curl_exec($ch);
curl_close ($ch);

参考:Facebook Graph API Post

  1. 确保您拥有为您的应用提供的有效访问令牌。

  2. 请求用户提供“publish_stream”权限。

  3. 编辑:

    以下是发布到用户墙的JavaScript方式

    function fb_publish() {
         FB.ui(
           {
             method: 'stream.publish',
             message: 'Message here.',
             attachment: {
               name: 'Name here',
               caption: 'Caption here.',
               description: (
                 'description here'
               ),
               href: 'url here'
             },
             action_links: [
               { text: 'Code', href: 'action url here' }
             ],
             user_prompt_message: 'Personal message here'
           },
           function(response) {
             if (response && response.post_id) {
               alert('Post was published.');
             } else {
               alert('Post was not published.');
             }
           }
        );  
    }