Post status/link/images to your timeline with php and graph api

by  
Share:
facebook

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 I will explain how to post status/link/images to your timeline with PHP and Graph API.

Get started: Create facebook application.

If you are a beginner you should check these post before going through this posts.

Permission required to post on user timeline

To post to user timeline you will need publish_actions permission. Also due to facebook february 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

To post a simple message to your timeline via graph api just use the following snippet.

<?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

To post a message with a link to your timeline via graph api just use the following snippet.

<?php
$success = FALSE;$error = FALSE;
$param = array(
    'message' => "your message goes here",
    'link' => "https://aarafacademy.com",
   );
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, caption to your timeline

To post a message, with image and caption to your timeline via graph api just use the following snippet.

<?php
$success = FALSE;$error = FALSE;
$param = array(
    'message' => "your message goes here",
    'picture' => "https://aarafacademy.com/wp-content/uploads/2014/03/facebook-680x250.jpg",
    'caption' => "facebook tutorials by thesoftwareguy",
   );
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;
?>

Share to download the Source Codes for FREE!
We're glad to give free downloads, but we need your love to carry on making that.
Please support us by sharing the page.

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.

Share:
Hasan

Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

Vignesh Ganguly on

Hi

I tried to execute this ..it’s working perfectly..and i spend two day for this concept..because of the version upgrading ..now i got the solution..

But the only problem is ..when i try to post my business page ..it ‘s not posted on the facebook business page wall.. it’s posted on Facebook business page left side..

Reply
Vignesh Ganguly on

It is possible to Convert HTML tag into Normal Text …..while post the status in Facebook

Example

my input is

$var=”a href=’url’ target=’_blank’>Click Here</a;

i need output

Click Here

Reply
Shahrukh Khan on

I dont think so that facebbook allows that.. U have to use actions url so that if some one clicks on your post so user will be redirected to that page.

Reply
sathya on

this is not working

Reply
Shahrukh Khan on

what is the error

Reply
Shirish on

Hi! I have tried your code it is working correct with posting on my timeline.
But I want to post on users timeline, so how I can achieve this with your code?

Reply
Shahrukh Khan on

that feature is been banned by the facebook

Reply
Agus Cahyono on

i have tried your code, for update status it’ working, but for update status with link and image not working. can you resolve this ? i dont know whats problem with this

Reply
Shahrukh Khan on

make sure the query string and the the image link are perfectly fine.

Reply
Denny on

iTs not working, it shows “The user hasn’t authorized the application to perform this action” even if I give all the permissions.

Reply
Shahrukh Khan on

i havent look facebook API for sometime, check their official docs for more info.

Reply
Tej Prakash on

I stuck in facebook app deveopement.
My app has permission to read email,public_profile and user_friends, but i want permission public_action to post message on wall.
I can post it from my account successfully.
but when any other user login into my application and try to post message than it requires permission.
I have requested for permission many times of publish_action, but all time facebook rejects my permission request.
it returns “The following items were not reviewed due to general issues with your app:”
but my application is fully configured and it is live to use for all users.
What is missing part?

I have already read how to request for permission.
Permissions with Facebook Login

I have follwed those steps .but it doesn’t work to approve my request.
Is anyone has been done like this before?
Any solution will be highly appriciated.

Reply
Shahrukh Khan on

I think you need to contact the faceboook developers for best results. It looks like they have the power on the permission.

Reply
sarvesh patel on

Error

(#200) The user hasn’t authorized the application to perform this action

please what is this

how can i solve this

Reply
Shahrukh Khan on

check the user permission. As per the lastest api changes I think you can only post to your wall not your friends wall.

Reply
Tej Prakash on

Last Section : Post Message,Image,caption to your timeline is not working
Show error : (#100) A link must be provided if a picture URL is given.

Reply
manohar on

error message plz help me
Notice: Undefined index: user_id in /storage/ssd3/923/5037923/public_html/home.php on line 10

Reply
Shahrukh Khan on

its notice not error. in php.ini turn off error notice.

Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.