Reputation: 1
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?
Upvotes: 1
Views: 415
Reputation: 10864
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
Make sure you have a valid access token which is given for your app.
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.');
}
}
);
}
Upvotes: 1