简体   繁体   中英

Taking a Facebook array of users - post to friends wall

I have an array of Facebook users, that have been outputted using a custom Facebook Multi-Selector.

The output is a list of the selected users.

How can I take these ID numbers, then post a message to their walls using Facebook API?

Below is a working PHP code snippet what I used before

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

Reference: Facebook Graph API Post

  1. Make sure you have a valid access token which is given for your app.

  2. Request the "publish_stream" permission from your users.

Edit:

Here is JavaScript way of publishing to a user's wall

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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