How to Post to your Facebook Wall using PHP and Graph API
If you are using facebook app and have some engaging users, posting activity to their wall can come in handy. Activity about their photos, likes and comments are the most common feature posted to their timeline. In this tutorial you will learn to post status/link/images to your timeline with php and graph api.
Live Demo
Get Started -> Create a Facebook Application
In case if you are a newbie, Watch the following Video before proceeding.
Permission required to post on users Timeline
To post to user timeline you will need “publish_actions” permission. Also due to Facebook Feb 6, 2013 changes you will not be able to post to your friends wall using graph api. You can only post to your timeline only.
Post Message to Your Timeline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $success = FALSE; $error = FALSE; $param = array( 'message' => "Your Message Goes Here...." ); try { $posted = $facebook->api('/me/feed/', 'post', $param); if (strlen($posted["id"]) > 0 ) $success = TRUE; } catch (FacebookApiException $e) { $errMsg = $e->getMessage(); $error = TRUE; } if ($success) echo "your message is successfully posted on your timeline"; elseif ($error) echo $errMsg; ?> |
Post Message and Link to Your Timeline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $success = FALSE; $error = FALSE; $param = array( 'message' => "Your Message Goes Here...", 'link' => "http://www.phphive.info", ); try { $posted = $facebook->api('/me/feed/', 'post', $param); if (strlen($posted["id"]) > 0 ) $success = TRUE; } catch (FacebookApiException $e) { $errMsg = $e->getMessage(); $error = TRUE; } if ($success) echo "your message is successfully posted on your timeline"; elseif ($error) echo $errMsg; ?> |
Post Message ,Image and Caption to Your Timeline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $success = FALSE; $error = FALSE; $param = array( 'message' => "Your Message Goes Here...", 'picture' => 'http://www.phphive.info/wp-content/uploads/2014/10/PHPHive.png', 'caption' => "PHPHive - PHP Lessons, Tutorials, Snippets and More", ); try { $posted = $facebook->api('/me/feed/', 'post', $param); if (strlen($posted["id"]) > 0 ) $success = TRUE; } catch (FacebookApiException $e) { $errMsg = $e->getMessage(); $error = TRUE; } if ($success) echo "your message is successfully posted on your timeline"; elseif ($error) echo $errMsg; ?> |
Demo Video
Live Demo
Thanks!
Your feedback helps us to improve PHPHive.info
11 Comments
Thanks for a great article , but there is an error message when submitted.. Thank You..
Can u Please Elaborate ? What Error Message you are Getting ?
Hi, error message is:
Oops Some Error Occured !
(#200) The user hasn’t authorized the application to perform this action
You Need to get Facebook App “publish_actions” permission Reviewed & Approved.
Posting using link and image error. can you resolve this, bro ?
Hi, error message is:
Oops Some Error Occured !
(#200) The user hasn’t authorized the application to perform this action
You Need to get Facebook App “publish_actions” permission Reviewed & Approved.
Awesome Job ……
thanks for the info
SQLSTATE[HY000] [1049] Unknown database ‘testdatabase’
Create a MySQL Database, Import db.sql
and Put those MySQL Details in Config.php
it’ll Work 🙂