The Facebook Documentation for PHP SDK 4.0 is basic at the moment, and doesn’t fully document common uses of the API. Therefore, I’m rewriting old tutorials to work with the new PHP SDK 4.0 and Graph API 2.0. This tutorial covers how to post a status update to the user’s timeline in the background using the API.
First off, you need to add the publish_actions
permission to your app to allow automatic posting of text, links, or photos to the timeline. This permission will need to be approved by Facebook before your users will be able to approve it. Once approved, your users will see a dialog similar to the one below:
In order to post a status update to the timeline, we must make a POST
call to the /{user_id}/feed
API endpoint. This endpoint remains unchanged from Graph API v1.0. The user must be logged in and have a valid access_token
for the API call to work.
For the logged in user, the endpoint we need is below. At the minimum, we need to include the message
parameter and access_token
. At the minimum, the message
parameter should be passed.
If successful, Facebook will return an ID for the post, which is made up of your User ID and the ID for the post.
Array ( [id] => 60506094_10100387108404056 )
If you want to post images, links etc, you can include additional parameters in your API call. The following are just a few parameters you can use:
message
– the text for the status update, e.g. “TGI Friday!”.link
– A URL to a website you want to link topicture
– URL of an image you want to include in the updatename
– The name of the link attachmentcaption
– The caption of the link (appears beneath the link name)description
– The description of the link (appears beneath the link caption)
For example, if I wanted to post a link to this article, my data would look like:
The status update itself will look something like:
A full list of properties to use with the /feed
API call can be found on Facebook’s Documentation for Feeds.
Hi there,
can we retrieve all the post from the particular group using facebook API v4.0.0 using php.
Yes, you can call the following endpoint:
{group_id}/feed
. If the group is private, you won’t be able to retrieve the posts.E.g.:
Thank you 🙂
Hi, thank you for your tutorials, really helped me !
But now, i block with page publishing, can you make a tutorial on it ? How can I use the access token of my pages to publish ?
Thanks
Nevermind… I read the tutorial again, and it’s ok, thank you again 🙂
Hello, been trying to understand this but I am really confused and I don’t know how to integrate it into my code. Would I be able to get the user to browse for a photo from their computer to upload to facebook? I followed the answer from this question: http://stackoverflow.com/questions/24317721/post-picture-facebook-graph-api-php but stuck step 2 and 3. It is also different in syntax to yours. I am confused. Would his solution work? I asked him for help but he wasn’t being helpful. I am so sorry to bother you. Thanks
The answer on StackOverflow is overly complicated. If you use my code together with the upload form on StackOverflow, you can achive the same result. Add in the
picture
parameter to the/me/feed
API call and you can upload a image directly to your timeline. You will obviously need to upload the photo to your server first, before it can be saved to Facebook.Hello, thank you for taking the time to look at the code… what do you mean upload the photo to the server first? Would I be able to do that via the form? So that the user (me) can browse and submit a picture from my computer and upload it to the server via my app and then onto facebook? (Sorry, probably really obvious what to do but I really slow….) Thanks again, for being really patient with me
You need to use HTML and a PHP script to upload the photo to your server before sending it to Facebook. Use Google to find some sample code on how to achieve this. The user will be able to browse for a photo, and your code will need to upload the photo, save it to the server and then send it to Facebook using the Graph API.
Also, using your code in a new file: upload.php, do I need to include all the facebook files again? Or do I simply just put because it’s showing errors, saying session is undefined
Yes, all the Facebook SDK files need to be included everywhere you intend to use Facebook. Otherwise, any attempt to call functions from the SDK will result in an error. To simplify things, you should using PHP Includes to easily include common code into each file (hint: Google how to do this too).
Okay! Thank you for the advice and hints! One more question, in the upload.php (or any other files I create) do I need to do the whole create session thing again and app id thing again? Thanks!
Yes, you need to repeat pretty much everything again. Ideally, you just need to make sure the user session is valid on this page.
Yay! I managed to upload a photo to the server! I think I’m getting a better idea how my app works now, thanks to you! But got a problem with the include.php thing, it doesn’t recognise it? I get the error that FacebookSession is not defined.. I copied and pasted all the require_once and use into a file called include.php and then in my index.php I called it include (‘include.php’); <– I've tried so many different syntaxes too! Where am I going wrong? Thanks! (sorry for spamming.)
Post your issue on StackOverflow and see if someone can help you out.
Okay, thanks for your help 🙂
Hi Niraj, I am trying to get the publish_actions permission for my app, but could not figure out where to obtain these extended right. I don’t want to make my app public right now and just want to play with it, so I do not want to submit anything for review. Is it possible that I give just extended permissions to the administrator of the app, or do I always need to submit my app for review in order to get the publish_actions permission.
Thanks a lot!
Lisa
Hi Niraj, I amnage to solve the problem and I can see now the post in a Facebook group where I am the admin. However, if I ask a friend if my post appears in the group, he tells me that it is not shown. I guess, it has something to do that my app is still running in the sandbox and I have to submit my app for review to facebook. Is this correct?
Do you know if facebook terms of use allow an app to post content to my own group?
Hi Lisa,
I’ve tested Group Status Updates and found that Status Updates from Sandboxed Apps do not appear in the group until it’s made public. You can easily make the app public without having to get the permissions reviewed by Facebook. You just need a contact email address in the settings and you can make the app available to the general public. There are no restrictions for apps for posting to groups.
Hello. I was able to post a status but then I sometimes get this error: Catchable fatal error: Argument 1 passed to Facebook\FacebookRequest::__construct() must be an instance of Facebook\FacebookSession, null given.. I tried to do it for ‘source’=>{‘image-data’} and I get the same error..
Your code should be something like
$request = new FacebookRequest( $session, 'GET', '/me' );
. The first parameter toFacebookRequest
needs to be$session
, from a valid Facebook Session.I have that for my first request. But I am making a second api request to post a photo. Somehow there is no longer a session when I call this request so I’ve created one again like this:
$session = new FacebookSession( $_SESSION[‘fb_token’]);
$request = new FacebookRequest(
$session,
‘POST’,
‘/me/photos’,
array (
‘source’ => file_get_contents($location.$name),
)
);
But I get errors.. Please can you have a look at my code on stackoverflow?
http://stackoverflow.com/questions/24885784/facebook-api-sdk-4-0-post-a-photo-onto-facebook
Thank you.
I’ve replied to your Stackoverflow Question. I found two issues that were causing issues – fix both and your code will work. Your session is being destroyed because you have
session_destroy()
in your code – that needs to be removed. As for the source, do this instead:'source' => '@' . $location . $name
.Perfect! it solve my problems, i was using $sess
Thank you
Great! Thanks a lot Niraj 🙂
Hi Niraj,
Again me 🙂 I tried now to read from the feed of a facebook group. As you described above this only works if the group is open. I wonder however, if there is no way to read the content of a private group, when I am a member of that group or even its admin. It looks a bit strange to me that I can post to closed groups when I am a member of this group, but cannot read their content. Any idea how this could work?
Thanks a lo!
Hi Niraj,
I managed to figure out myself :). Do you know if it is possible to reply (publicly) in the group to posts of other people? Is this possible or does my app need special rights for this?
Cheers, Lisa
Hi Lisa,
It should be possible to reply to posts within a group (close, public, secret), but it would be publicly. Only members of the group will be able to see the reply, unless it’s an open group. I haven’t tried this myself so cannot confirm. You will only need to
user_groups
andpublish_actions
permissions.Hi Niraj,
but for the publish_actions permission I need to submit my app for review, no?
Yes, once you have developed your app, the permission will need to be reviewed before users can use it.
Hey, I’m currently just testing and configuring my application and my end goal is to be able to post on a users wall.
I understand that we need to get approval for publish_actions. How hard is it to actually get approval, and can I get approval without having a full application/website? I would really just like to use permissions to test some features I am working on a for a potential application.
Thanks!
You can build a website or app without getting approval for any permissions you require, the only drawback is that those permissions will only be available to application administrators, developers and testers. It does mean that you can test any feature you want requiring any permissions you want. You should only submit your application for review once it’s fully functionality. Facebook has said reviewing can take up to 2 weeks.
Hi Niraj,
I tried the following code to reply to one of my own posts in a test group I created:
When I execute the code, then I get the exception saying that “Unknown path components:..”.
Any idea how I can fix this?
Many thanks!
Lisa
You are using the incorrect path to the Group Post. Try this instead:
Is it possible to change the privacy settings of the post when making the api request? I know at the start, the user accepts to share with friends… but can it be changed for individual posts?
Yes, you can override the privacy setting for individual posts using an additional parameter (see documentation). However, the user still determines the max privacy level. I.e. if a friend sets max privacy to Friends, your app cannot make the post public or viewable to Friends of Friends.
Do you mean the FQL? Am I going to have to do a database to do that? I did the additional parameter like this: ‘privacy’ => ‘{value: “EVERYONE”}’ but I don’t think that’s the one you’re talking about… because it doesn’t override the initial privacy setting. I want the user to be able to determine the privacy level of each post.
No, do not use FQL – it will be deprecated soon, and it has nothing to do with having a database. You need to use the
privacy
parameter as you’ve mentioned. As I said, it won’t override the maximum value the user has set when they first authorise your application. Your users would need to set max privacy level topublic
if they want to be able to use all the privacy levels, otherwise you will be limited to what they set.If I set the max level to
SELF
(i.e. only me), no matter what I set in the privacy level in the post, it will be limited to only me (as that is the max privacy level I’ve allowed the app to use).Oh right. So that initial privacy thing is the max privacy. So there is no way round it, THEY have to select public at the start? hmmm okay. You’re really helpful! Thank you!!!
Hi,
thanks for at great blog.
How do I check which permissions are granted with the new PHP SDK? I need to check if ‘manage_pages’ is granted for my WEB-app.
You can make the following API call to see which permissions have been granted by the user:
Dear Niraj,
When I post to a group it always shows “posted via TestApp”. Can I somehow prevent this and make the post look like as ti was done as a normal user?
Regards,
Lisa
Hi Lisa,
No, there is no way to disable this. It’s something Facebook puts in so users know the origin of the post. I’ve seen developers try to circumvent this by changing the name of their app to
.
.Hi mr Niraj ..
How to request permission for publish_action explanation text and instructions text in facebook application ?
I am newbie n please help me to my problem
Thanks before
Please see this tutorial on how to add permissions to the login link. To get the permission approved, you need to include clear step-by-step instructions on how the permission is used.
Hi there, is it possible to create a new post that will be hidden in the users timeline. It will only be shown to the friends of the user but not on their timeline. Thanks.
No, you cannot hide content on the user’s timeline and have it visible to friends. You can do it the other way around, with content hidden to friends.
Yesterday I found that there exists an option called “no_feed_story” property when posting using the Graph Api, but I haven’t tested it yet, maybe it works….
The
no_feed_story
parameter is actually used to suppress Facebook from creating newsfeed posts when uploading photos, setting cover images or publishing OpenGraph actions. It means the post isn’t published to the user’s feed, which also effectively means friends don’t see it either. It’s covered in my “Updating the Page Cover Image Programmatically” tutorial.I also saw a script that someone was selling, and he wrote that the script does the following:
– when the user visits the website, after authorising the Facebook Application it automatically creates a new posts that will not be shown on the user timeline, but it will still appear on ticker and users friend.
Do you think this is possible? Or he was lying.
thanks.
As my understand of the Facebook platform goes, it’s not possible to show something to friends unless it’s also visible on the user’s timeline. Also, this application you speak off is against Facebook ToS as apps aren’t allowed to post to the user’s timeline without explicit permission. Accepting the
publish_actions
permission isn’t consent.Hi Niraj,
How do I set cronjob for posting on my Facebook admin page using graph api php sdk 4.0 ?
Write the script to publish to page as you normally would, but hard-code in the page
access_token
. You can then call the script via command-line to test, and then turn it into a cron (which will call the command-line on a scheduled basis). You’ll have to ask your hosting provider for instructions on setting up cronjobs (I set them up usingcrontab -e
on command line, but you may have access to a interface)Hi Niraj,
Thank you for your quick reply. 🙂
Hi Niraj,
Nice article !
I’m wondering what are the conditions to meet for Facebook to accept the review of “taggable_friends” and “publish_actions” permission.
I’m exactly in the case your sample where you suggest a friend to take a look to your new tutorial on your blog.
Do you think the review of this 2 permissions can be accepted by Facebook for such a usage (suggesting a friend to come to your site) ?
Thanks a lot…
Fred
As long as you follow the guidelines, the permissions will get approved. For
publish_actions
actions, that means sharing to the timeline only if the user has explicitly allowed you to do so. Make sure you provide detailed instructions on how each permission is used and where to find it in your application.hi, can you help me, how can i display my facebook wall post from people and show the comment link like facebook?
This is not easily doable. You would need to recreate all the facebook functionality found on the website, and you’d be revealing your private Facebook stream to users. Not really worth the effort.
Hello, can I retrieve all my group ids using facebook API v4.0.0 using php. Thank you
You can make an API call to
me/groups
(with the user logged in and `user_groups` permission). This will return a list of all the groups the user is a member of.Thank you so much. I did it but I encounter with another problem when I am trying to do 2 section. For example, I try to get group id and after that I want to use that ID to get post from that group. Is my idea possible?
Once you have the Group IDs, you can loop through them and extract the posts using:
/{group_id}/feed
However, please note that Groups have been removed from the API in v2.4, and will no longer work from October 6, 2015.
$request = new FacebookRequest(
$session,
‘POST’,
‘/xxxxxxxxxxxxxxxxxxx/members/’.$_SESSION[‘FBID’],
array(‘access_token’=>$token)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
echo “”;
print_r($request);
echo “”;
I have got this error and i had used GET and members=groups than get all data.
Error look like –
Fatal error: Uncaught exception ‘Facebook\FacebookAuthorizationException’ with message ‘(#3) Unknown method’ in
The error says that the API call you are making doesn’t exist – this is because you cannot POST to
group_id/members
. What happens if you make a GET call instead? And what exactly are you trying to do?