Search the site...
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.
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.
View the code on Gist.
If successful, Facebook will return an ID for the post, which is made up of your User ID and the ID for the post.
message – the text for the status update, e.g. “TGI Friday!”.
View the code on Gist.
A full list of properties to use with the /feed API call can be found on Facebook’s Documentation for Feeds.
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.
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 ?
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.
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.
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.
Post your issue on StackOverflow and see if someone can help you out.
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!
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?
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 to FacebookRequest needs to be $session, from a valid Facebook Session.
But I get errors.. Please can you have a look at my code on stackoverflow?
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.
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!
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?
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 and publish_actions permissions.
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.
When I execute the code, then I get the exception saying that “Unknown path components:..”.
Any idea how I can fix this?
Many thanks!
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 to public 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!!!
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.
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?
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 ?
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.
– 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.
How do I set cronjob for posting on my Facebook admin page using graph api php sdk 4.0 ?
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) ?
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.
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?
However, please note that Groups have been removed from the API in v2.4, and will no longer work from October 6, 2015.
I have got this error and i had used GET and members=groups than get all data.
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?
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Become a Patron!
