posting on a friend's wall using graph API - php

Posting a message on friend's wall, graph API.
i have the publish_stream extended permission of the user, who is using the application.
the code workds if i want to post sth on my wall.
is there any method to post on wall or send message to all the friends of a particular user??
please help thanks!!
following is the code, but it is not working.
$friends = $facebook->api('/me/friends');
foreach($friends['data'] as $friend){
$friendsUserId = $friend['id'];
echo $friendsUserId . "</br>";
$result = $facebook->api('/$friendsUserId/feed', 'POST', array(
message' => 'Test Message' ));
print_r($result);
}

This would perhaps be the way to do it.. (Untested, but I use something similar for my app.)
$friends = $facebook->api('/me/friends');
foreach($friends['data'] as $friend) {
$facebook->api('/$friend['id']/feed', 'post', array(
'message' => 'just a test message',
'link' => 'http://site.com',
'name' => 'just a test name',
'caption' => 'just a test caption',
'description' => 'just a test description',
));
}
This is using the PHP API.

I just did a little testing, and having the permissions of the user, I could indeed post on the user's friends walls via the php api:
$facebook->api('/[FRIEND_ID]/feed', 'post', array(
'message' => 'test message',
'link' => 'http://google.com',
'name' => 'test name',
'caption' => 'test caption',
'description' => 'test long description',
));

The publish_stream permission only allows you to publish content on that particular users wall - you cannot post on the wall of all his friends.
Think about it this way - say you have not authorized an application to post anything on your facebook wall but just because one of your friends has given access to his wall - it automatically does not give the application access to publish on the wall of all his friends.
EDIT
Rahul, I tried yesterday night to login as myself and post a message on the wall of one of my friends through my app and surprisingly it worked. I was under the wrong impression that you could not do that. My apologies.
But I'm still not able to figure this out. Let me explain
1) I have my real facebook lets call it abc and a test facebook account xyz which I've added as my friend to abc
2) I login into my app using my abc faceboook account and publish a message on the wall of my friend xyz
3) Now I login to facebook.com as abc and it shows on my news feed the message that I published on xyz's wall. When I click on xyz's profile - the message shows up on his wall too. So far so good
4) Now I log out of facebook.com as abc and log back in as xyz. But now I dont see the message that was posted through the app on the wall of xyz.
I dont know if there is any kind of delay but I've waited 30 mins but still its not there. But it continues to show when I log in as abc.
I hope you understand what I'm trying to convey here.
I've used the same piece of code like yours - so can you try the above scenarios and see if you experience something similar
Thanks

Gubloo's answer is close.
Posting to a friend's wall is completely possible, but with one caveat, said friend must have also gone through the 'Permissions' dialogue at some point. Meaning, users of your application will be able to post to the wall's of other users of your application.
I'm going to give you the iPhone code, because I don't know php and imagine you'll get the gist. Just replace 'me' with the UID of whatever you are trying to post to, be a page, or a Friend's profile.
[_facebook requestWithGraphPath:#"/me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
I'm actually curious to know if this would give the user the ability to post on the wall of anyone who uses the application, and not just his friends.

$friends = $facebook->api('/me/friends');
foreach($friends['data'] as $friend) {
$facebook->api('/$friend['id']/feed', 'post', array(
'message' => 'just a test message',
'link' => 'sample link.com',
'name' => 'just a test name',
'caption' => 'just a test caption',
'description' => 'just a test description',
));
}
Above code work for me tooooo
Thanks for the post

Related

Posting Message using Facebook API

Couldn't find this anywhere on Stack overflow, I just don't know what to do.
I just want to place a message on my community Facebook page using the Facebook PHP API, where to get started?
Example:
I click a button on my website, which then automatically posts 'I clicked the button' on my Facebook (community) page.
Thanks in advance!
There is documentation for what you need.
$session, 'POST', '/me/feed', array(
'link' => 'www.example.com',
'message' => 'User provided message'
)
Change /me/feed to the page you want.

Facebook Graph Api posting on friends wall

I'm having a problem when trying to post a link to a friends wall using the Facebook graph from my application.
I am currently using the Facebook SDK for PHP, I have no problems posting to the wall of the user that's logged in but cannot post to friends of the logged in user.
I have requested the extended permission "publish_stream" and here is the code am I using:
$args = array('message' => $message,
'link' => 'google.com',
'name' => 'Test!',
'caption' => 'Please click on the link',
'description' => 'description');
$result = $facebook->api("/$friendId/feed", 'POST', $args );
$friendId has the facebook id of the friend of the logged in user, any help would be much appreciated.
A quick scan of the FB API reference shows that you can set the to key value pair in this operation to
mention or target a user in this post

Post in the wall of a Facebook user

My question is very simple and has been answered lots of times in Stackoverflow but in order to the changing of the Facebook API I want to make certain if a user who is using my app can post to their wall a message generated automatically by the app.
If the user is reading in my app a comment he/she likes, I want he/she can post to their wall a message like "I've been reading a very interesting comment about..." and I would like that tis text appears automatically in the window where Facebook requests permission to the user to post in their wall.
I've tried the JS API with "feed" method but the text in the field message is ignored. I'm going to try with PHP API but I think the result will be the same.
Thanks in advance!!
JavaScript SDK
You can send the following values using the JavaScript SDK:
// calling the API ...
var obj = {
method: 'feed',
link: 'https://developers.facebook.com/docs/reference/dialogs/',
picture: 'http://fbrell.com/f8.jpg',
name: 'Facebook Dialogs',
caption: 'Reference Documentation',
description: 'Using Dialogs to interact with users.'
};
(code and image taken from the docs)
I described, what field is what text in the popup.
If not provided, name, image, caption and description are derived from the webpage, given in the link.
You cannot include a default message in the JS popup.
PHP SDK
If you are using the PHP SDK, you can however include a message.
Code example:
try {
$facebook->api(
"/{$facebookId}/feed",
"POST",
array(
// this is the important part
'message' => "This is your message!",
'link' => $yourLink,
'name' => "This is your name",
'caption' => '..',
'description' => '...',
'type' => 'link',
'application' => array(
'name' => 'Name of your app',
'id' => $idOfYourApp
)
)
);
}
catch (\FacebookApiException $exception)
{
// ...
}
the "message" field in feed no longer works. You have to use "description" or "caption"
This is because Facebook mentioned something along the lines of "A user should be able to say what they want about content on the internet".

How can I add information about read article on my site to facebook RECENT ACTIVITY?

How can I add information about read article on my site to facebook RECENT ACTIVITY (like the Guardian)?
I use php and code:
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'example.com',
'message' => 'Test message'
));
This code posting the message in user's timeline but I want to post message in his recent activity.
probably you wan't to use open graph api, not graph api
https://developers.facebook.com/docs/opengraph/
https://developers.facebook.com/docs/opengraph/tutorial/
https://developers.facebook.com/docs/opengraph/opengraph-approval/

programmatically login to facebook and post to page (wall)

I have a website, and I need it to post status updates to a Facebook Page from time to time.
Using my personal Facebook account, I created an App, and a Page. So far, I've been able to programmatically post to my Page's Wall, by adding this code to my website:
include_once "lib/facebook/src/facebook.php";
$facebook = new Facebook(array('appId' => 'APP_ID_HERE', 'secret' => 'APP_SECRET_HERE'));
if($facebook->getUser()) {
try {
$ret_obj = $facebook->api('/FACEBOOK_PAGE_ID_HERE/feed', 'POST', array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!',
'access_token' => 'FACEBOOK_PAGE_ACCESS_TOKEN_HERE'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// user logged out (has user_id, but invalid access token)
$login_url = $facebook->getLoginUrl(array('scope' => 'publish_stream'));
echo 'Please login.';
}
echo '<br />logout';
} else {
$login_url = $facebook->getLoginUrl(array('scope' => 'publish_stream'));
echo 'Please login.';
}
So I just open my website, click "Please login", login as myself. Once I'm logged it, it will now be able to post the status update to the Facebook Page.
Obviously, the problem here is that I need to be logged in for it to be able to post. If other users will try to login with their user accounts, my website cannot post status updates to the Facebook Page because I am the only Admin for the app/Page.
My question is, is there a way for me to programmatically log myself into Facebook so I can do these status updates to my Page automatically?
Sorry, total noob here to Facebook development.
Seems like you need to update status while you are offline from facebook. To do this you need to get the offline access permission of the app user and you need to have a "infinite" token for facebook app to access the APIs, so that your program can update anytime you want with out logging into facebook.
You may get something you want here
http://developers.facebook.com/docs/authentication/
I'm a little late to the party here, but I thought I'd post my gist example for how to do something very similar. I added some functionality to fbconsole to make it easy to programmatically login with fbconsole.automatically_authenticate to make it much easier to access this information in a systematic way. This addition has not yet been incorporated into the master branch of fbconsole (it was just posted this morning), but it is available here in the meantime for those that are interested.
This is what I use to do,
// = SET WALL DATA HERE ===============
$attachment = array(
'access_token' => 'My Access token here',
'message' => '',
'name' => 'My Wall Post Header/Title Here',
'caption' => 'Small caption here',
'link' => 'http://www.mywebsite.org',
'description' => 'Wall Post Details Here',
'picture' => "http://www.mywebsite.org/images/logo.gif",
);
// =POST ON FACEBOOK WALL ==========================
$this->facebook->api('/me/feed', 'POST', $attachment);
, $attachment);
So include your Facebook API, Get your Facebook Access token, SET data for your wall post and call for facebook api with me/feed method.
This will make a wall post on your wall without asking for login.
make sure you are using correct Access Token.

Categories