If you want your web application to post links on the Facebook timeline then this article is for you. In this tutorial I will tell you how to post a link on your Facebook timeline with an image. Here we will post an image with the link. Note that Facebook shows the image posted with the link from its original location i.e the image is not actually uploaded on Facebook server. So you need to upload the image to a publicly accessible server so that it can be accessed by Facebook. Here is the live demo.
Live Demo Download Script
Live Demo Download Script
Complete PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>WebSpeaks.in | Post link on Facebook Timeline</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
require_once 'library/facebook.php';
try{
$app_id = "xxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
'fileUpload' => true
));
if(is_null($facebook->getUser())) {
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
exit;
}
$me = $facebook->api('/me');
}catch(Exception $e){
echo $e->getMessage();
echo '<p>Please try clearing your browser cookies or <a href="http://demos.webspeaks.in/fbAlbum/fblink.php">click here</a>.</p>';
die;
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if ($_FILES["pic"]["size"] < 2000000) {
try{
if ((($_FILES["pic"]["type"]=="image/gif") || ($_FILES["pic"]["type"]=="image/jpeg") || ($_FILES["pic"]["type"]=="image/png"))) {
if ($_FILES["pic"]["error"] > 0) {
$errors .= "Return Code: " . $_FILES["pic"]["error"] . "<br />";die;
} else {
move_uploaded_file($_FILES["pic"]["tmp_name"], "uploads/" . $_FILES["pic"]["name"]);
}
}
}catch(Exception $e){
echo '<br>',$e->getMessage();
}
} else {
echo '<br>File size exceeded.';
}
try{
$facebook->setFileUploadSupport("http://" . $_SERVER['SERVER_NAME']);
$attachment = array(
'access_token' => $facebook->getAccessToken(),
'message' => $_POST['msg'],
'name' => $_POST['caption'],
'source' => 'http://' . $_SERVER['HTTP_HOST'] . '/fbAlbum/uploads/' . $_FILES["pic"]["name"],
'link' => $_POST['link'],
'description' => $_POST['desc']
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$me['id'].'/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
echo '<br>Link posted on your timeline.';
echo "<br><a target='_blank' href='https://www.facebook.com/".$me['id']."/'>See this on your timeline<a>.";
}catch(Exception $e){
echo '<p>Some error occurred.</p>';
echo $e->getMessage();
}
}
?>
<div class="main">
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p>Select a photo to upload on Facebook.</p>
<p>Select the image: <input type="file" name="pic" /></p>
<p>Photo Caption: <input type="text" class="text" name="caption" /></p>
<p>Photo Link: <input type="text" class="text" name="link" value="www.webspeaks.in" /></p>
<p>Message: <input type="text" class="text" name="msg" /></p>
<p>Photo description: <input type="text" class="text" name="desc" /></p>
<p><input class="post_but" type="submit" value="Upload to my album" /></p>
</form>
</div>
</body>
</html>
A bit of CSS:
<?php
html{
font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
}
.main{
width:400px;
margin:auto;
border:2px solid #0066CC;
color:#3B5998;
padding:20px;
font-size: 11px;
-moz-border-radius: 4px 4px 4px 4px;
border-radius: 4px 4px 4px 4px;
-moz-box-shadow: 1px 1px 0 #d5d5d5;
background: none repeat scroll 0 0 #F2F2F2;
}
.text{
color: #777777;
border: 1px solid #BDC7D8;
font-size: 11px;
height: 15px;
width:150px;
}
.post_but {
background: none repeat scroll 0 0 #EEEEEE;
border-color: #999999 #999999 #888888;
border-style: solid;
border-width: 1px;
color: #333333;
cursor: pointer;
display: inline-block;
font-size: 11px;
font-weight: bold;
padding: 2px 6px;
text-align: center;
text-decoration: none;
}
a{
color:#3B5998;
}
?>
Sweet blog! I found it while browsing on Yahoo News.
Do you have any tips on how to get listed in Yahoo News?
I’ve been trying for a while but I never seem to get there! Thank you
Also visit my web site – Absence of the cuticle
Hi, is great your post, i will integrate to my page and ping here datails.
thanks for all information.
in my favorites