Facebook Share Button Functionality - facebook

I want to add Facebook share button in my website so that user can share any post ,content of given url.
I want to know how can I get that either user is shared something or not on Facebook because I have to store user data in database if user share any post,content of given url from my website.
<a href="https://www.facebook.com/sharer/sharer.php?u=example.org" target="_blank">
Share on Facebook
</a>

I have found a solution .
code :-
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<title>My Feed Dialog Page</title>
<script src="http://code.jquery.com/jquery-latest.pack.js" type="text/javascript"></script>
</head>
<body>
<div id='fb-root'></div>
<script src='http://connect.facebook.net/en_US/all.js'></script>
<a onclick='postToFeed(); return false;' style="cursor:pointer;"><img src="img/xyz.jpg"></a>
<p id='msg' style="display:none"><?php echo $_REQUEST['id'] ?></p>
<script>
FB.init({appId: "your app id", status: true, cookie: true});
function postToFeed() {
var obj = {
method: 'feed',
//redirect_uri: 'YOUR URL HERE',
link: 'https://www.abc.in/',
picture: 'http://fbrell.com/f8.jpg',
name: 'abc',
caption: 'User Has Won points ',
description: 'Share Test.'
};
function callback(response) {
//document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
var id=$("p").html();
if(response['post_id']!=null)
{
$.ajax({
url:'customer.php',
data:'fbpoints='+'1'+'&customer_id='+id,
dataType:'html',
success:function(res){ alert(res);
}
});
}
}
FB.ui(obj, callback);
}
</script>
</body>
</html>

Related

Facebook App Like server error?

I have a sample app that has a 'Like' button. It works before but now it doesn't.
I've tested this on jsfiddle.net before and it works too. But after I've uploaded my files on a server it has a bug. The 'Like' button pops up and then has this error:
https://www.facebook.com/plugins/error/confirm/like?iframe_referer=https%3A%2F%2Fns2383.hostgator.com%2F~caryucla%2Fbpcontest%2FHTML%2FContestHome.php%2F&secure=true&plugin=like&return_params=%7B%22api_key%22%3A%22301719366592208%22%2C%22locale%22%3A%22en_US%22%2C%22sdk%22%3A%22joey%22%2C%22ref%22%3A%22.UKWNYwbFB-8.like%22%2C%22channel_url%22%3A%22https%3A%2F%2Fs-static.ak.facebook.com%2Fconnect%2Fxd_arbiter.php%3Fversion%3D17%23cb%3Df13bb8c95%26origin%3Dhttps%253A%252F%252Fns2383.hostgator.com%252Ff2bf9f920%26domain%3Dns2383.hostgator.com%26relation%3Dparent.parent%22%2C%22href%22%3A%22https%3A%2F%2Fwww.facebook.com%2Fpages%2FDummy-Page-for-Testing%2F375795832160%22%2C%22node_type%22%3A%22link%22%2C%22width%22%3A%2290%22%2C%22font%22%3A%22arial%22%2C%22layout%22%3A%22button_count%22%2C%22colorscheme%22%3A%22light%22%2C%22action%22%3A%22like%22%2C%22show_faces%22%3A%22false%22%2C%22send%22%3A%22false%22%2C%22extended_social_context%22%3A%22false%22%2C%22ret%22%3A%22sentry%22%2C%22act%22%3A%22connect%22%7D
I've already read this article on SO already and followed the answer but still didn't work. Any views on this error?
PS:
I am all in all fairly new to this.
and here's the sample code I'm using:
<?php
include_once("../incs/dbcon.php");
require_once('AppInfo.php');
require_once('utils.php');
require_once('../sdk/src/facebook.php');
//This gets the signed_request from FB which checks the 'liked' status on the current page
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
$app_data = isset($data["app_data"]) ? $data["app_data"] : '';
$_REQUEST["fb_page_id"] = $data["page"]["id"];
$access_admin = $data["page"]["admin"] == 1;
$has_liked = $data["page"]["liked"] == 1;
$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
));
$user_id = $facebook->getUser();
if ($user_id) {
try {
// Fetch the viewer's basic information
$basic = $facebook->api('/me');
} catch (FacebookApiException $e) {
// If the call fails we check if we still have a user. The user will be
// cleared if the error is because of an invalid accesstoken
if (!$facebook->getUser()) {
header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
exit();
}
}
// This fetches some things that you like . 'limit=*" only returns * values.
// To see the format of the data you are retrieving, use the "Graph API
// Explorer" which is at https://developers.facebook.com/tools/explorer/
$likes = idx($facebook->api('/me/likes?limit=4'), 'data', array());
// This fetches 4 of your friends.
$friends = idx($facebook->api('/me/friends?limit=4'), 'data', array());
// And this returns 16 of your photos.
$photos = idx($facebook->api('/me/photos?limit=16'), 'data', array());
// Here is an example of a FQL call that fetches all of your friends that are
// using this app
$app_using_friends = $facebook->api(array(
'method' => 'fql.query',
'query' => 'SELECT uid, name FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1'
));
}
// Fetch the basic info of the app that they are using
$app_info = $facebook->api('/'. AppInfo::appID());
$app_name = idx($app_info, 'name', '');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:fb="http://ogp.me/ns/fb#">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo he($app_name); ?></title>
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# bpapptestbpapptest: http://ogp.me/ns/fb/bpapptestbpapptest#">
<meta property="fb:app_id" content="301719366592208" />
<meta property="og:type" content="bpapptestbpapptest:video" />
<meta property="og:url" content="Put your own URL to the object here" />
<meta property="og:title" content="Sample Video" />
<meta property="og:image" content="https://ns2383.hostgator.com/~caryucla/bpcontest/Images/BPIcon.png" />
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# object: http://ogp.me/ns/object#">
<meta property="fb:app_id" content="301719366592208" />
<meta property="og:type" content="object" />
<meta property="og:url" content="Put your own URL to the object here" />
<meta property="og:title" content="Sample Object" />
<meta property="og:image" content="https://ns2383.hostgator.com/~caryucla/bpcontest/Images/BPIcon.png" />
<link href="https://ns2383.hostgator.com/~caryucla/bpcontest/CSS/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function logResponse(response) {
if (console && console.log) {
console.log('The response was', response);
}
}
$(function(){
// Set up so we handle click on the buttons
$('#postToWall').click(function() {
FB.ui(
{
method : 'feed',
link : $(this).attr('data-url')
},
function (response) {
// If response is null the user canceled the dialog
if (response != null) {
logResponse(response);
}
}
);
});
$('#sendToFriends').click(function() {
FB.ui(
{
method : 'send',
link : $(this).attr('data-url')
},
function (response) {
// If response is null the user canceled the dialog
if (response != null) {
logResponse(response);
}
}
);
});
$('#sendRequest').click(function() {
FB.ui(
{
method : 'apprequests',
message : $(this).attr('data-message')
},
function (response) {
// If response is null the user canceled the dialog
if (response != null) {
logResponse(response);
}
}
);
});
});
</script>
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '301719366592208', // App ID
channelUrl : ' https://ns2383.hostgator.com/~caryucla/bpcontest/HTML/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true,
xfbml : true // parse XFBML
});
// Listen to the auth.login which will be called when the user logs in using the Login button
//FB.Event.subscribe('auth.login', function(response) {
// We want to reload the page now so PHP can read the cookie that the
// Javascript SDK sat. But we don't want to use
// window.location.reload() because if this is in a canvas there was a
// post made to this page and a reload will trigger a message to the
// user asking if they want to send data again.
//window.location = window.location;});
//FB.Canvas.setAutoGrow();
FB.Event.subscribe('auth.authResponseChange', function(response) {
alert('The status of the session is: ' + response.status);});
};
</script>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1&appId=301719366592208";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<link rel="stylesheet" href="https://ns2383.hostgator.com/~caryucla/bpcontest/CSS/jquery-ui.css" /><!--http://code.jquery.com/ui/1.9.1/themes/base/jquery-ui.css-->
<script src="https://ns2383.hostgator.com/~caryucla/bpcontest/javascript/jquery-1.8.2.js"></script><!--http://code.jquery.com/jquery-1.8.2.js-->
<script src="https://ns2383.hostgator.com/~caryucla/bpcontest/javascript/jquery-ui.js"></script><!--http://code.jquery.com/ui/1.9.1/jquery-ui.js-->
<script src="https://ns2383.hostgator.com/~caryucla/bpcontest/javascript/jquery.bgiframe-2.1.2.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
FB.Event.subscribe('edge.create',
function(response) {
$(function onclick() {
$( "#dialog-confirm" ).dialog({
resizable: false,
height:250,
modal: true,
buttons: {
"Register?": function() {
window.location="https://ns2383.hostgator.com/~caryucla/bpcontest/HTML/RegForm.php";
$( this ).dialog( "close" );
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
});
}
);
</script>
</head>
<body>
<div id="Content">
<div id="header">
<div id="LogoDiv">
<img id="logo" src=" https://ns2383.hostgator.com/~caryucla/bpcontest/Images/Bp logo.png"></div><!--end of LogoDiv-->
<div id="LogoTextDiv">
<p id="LogoText">Fashion & Compassion Contest</p></div><!--end of LogoTextDiv-->
</div><!--end of header-->
<div id="DummyRow1"></div><!--end of DummyRow-->
<div id="BGForm">
<div id="Home">
<div id="Welcome">
<h1 id="WelcomeText">Welcome to Banana Peel's Fashion and Compassion Contest App!</h1>
</div><!--end of Welcome-->
<div id="HomeMechanics">
<h2>Contest Mechanics:</h2>
<div id="FBLogIn" class="fb-login-button" data-show-faces="true" data-width="200" data-max-rows="1" autologoutlink="true" onlogin="OnRequestPermission();"></div><!--end of FBLogIn div-->
<p id="Mec">To join the contest, Participants must first Log In through Facebook and LIKE Banana Peel's Facebook Fan Page. After liking the page you will be able to click the registration link button.</p>
<div id="LikeBox">
<div class="addthis_toolbox addthis_default_style ">
<a class="addthis_button_facebook_like" fb:like:layout="button_count" addthis:url="https://www.facebook.com/pages/Dummy-Page-for-Testing/375795832160" addthis:title="Banana Peel Page" onclick="onclick()"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_button_pinterest_pinit"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
<div id="dialog-confirm">
<p><span class="ui-icon ui-icon-alert" style="float:left; margin: 0 7px 20px 0;"></span>Thank you for liking our page!</p>
</div><!--end of dialog-confirm div--><!--end of class addthis_toolbox addthis_default_style-->
<script type="text/javascript" src="https://ns2383.hostgator.com/~caryucla/bpcontest/javascript/addthis_widget.js"></script>
</div><!--end of LikeBoxhttp://s7.addthis.com/js/300/addthis_widget.js#pubid=xa-50a0584951afe01c-->
</div><!--end of HomeMechanics-->
<div id="ContestantsBG">
<img src="https://ns2383.hostgator.com/~caryucla/bpcontest/Images/BPpic.jpg"/>
</div><!--end of ContestantBG-->
<div id="GalleryButton">
<a id="BackLink" href="../HTML/ContestantGallery.php"><input type="button" value="VIEW GALLERY OF CONTESTANTS"></a>
</div><!--end of GalleryButton-->
</div><!--end of Home-->
</div><!--end of BGForm-->
</div><!--end of Content-->
</body>
</html>
This message is what Facebook use to stop Like-jacking and prevent spam. It can also appear if the Like button has been added to a website / domain for the first time. Give it time and the message will disappear once Facebook is satisfied that your site isn't doing any of the above things.

Facebook application requests

i am trying to implement a facebook request in a page tab application. i have seen the tutorial here http://developers.facebook.com/docs/appsonfacebook/tutorial/ but the problem is that i want the code below to be executed only when someone accesses a link like
Send to friends
any idea about how can this be done? thanks!
$requests_url = "http://www.facebook.com/dialog/apprequests?app_id="
. $app_id . "&redirect_uri=" . urlencode($the_url_of_the_tab_page)
. "&message=" . $message;
if (empty($_REQUEST["request_ids"])) {
echo("<script> top.location.href='" . $requests_url . "'</script>");
} else {
echo "Request Ids: ";
print_r($_REQUEST["request_ids"]);
}
The request dialog is what you are looking for:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<title>Request Tester C</title>
</head>
<body>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<p>
<input type="button"
onclick="sendRequestToRecipients(); return false;"
value="Send Request to Users Directly"
/>
<input type="text" value="User ID" name="user_ids" />
</p>
<p>
<input type="button"
onclick="sendRequestViaMultiFriendSelector(); return false;"
value="Send Request to Many Users with MFS"
/>
</p>
<script>
FB.init({
appId : 'YOUR_APP_ID',
status : true,
cookie : true,
oauth: true
});
function sendRequestToRecipients() {
var user_ids = document.getElementsByName("user_ids")[0].value;
FB.ui({method: 'apprequests',
message: 'My Great Request',
to: user_ids,
}, requestCallback);
}
function sendRequestViaMultiFriendSelector() {
FB.ui({method: 'apprequests',
message: 'My Great Request'
}, requestCallback);
}
function requestCallback(response) {
// Handle callback here
}
</script>
</body>
</html>
So your "link" shouldn't be calling a real url but instead a Javascript function to open the Request Dialog (in the above example it'll be sendRequestViaMultiFriendSelector()).

Facebook Login : How do I get the users email?

I'm new to the facebook api, but I have this:
<html>
<head>
<title>My Facebook Login Page</title>
</head>
<body>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId:'my_app_id', cookie:true,
status:true, xfbml:true
});
</script>
<fb:login-button perms="email">Login</fb:login-button>
</body>
I would like to know, when a user accepts my perms or when they return to my site, how do I get that users email adderss?
this will help :
FB.api('/me', function(user) {
if(user != null){
document.write(user.email);
}
})
call this function after user gives you permission
Check out the javascript sdk documentation, they have a similar example of doing this. Try something like:
FB.api(
{
method: 'fql.query',
query: 'SELECT email FROM user WHERE uid='+FB.getSession().uid
},
function(response) {
alert('Email is ' +response[0].email);
}
);

How to post simple text to FB wall programmatically using HTML?

What I'd like to do is to post some simple text to a user's FB wall (with the user's ID already known to my app).
Have read quite a few posts about posting to FB wall, however, still stumbled.
From the sample code at a youtube video, I created a complete code set and posted to
FB dev forum, the err msg I get is,
"FB.getLoginStatus() called before calling FB.init()"
Here's the link of my code,
http://forum.developers.facebook.net/viewtopic.php?id=94801
Note:
We could use a simple HTML page for testing this...
What's wrong? Thanks.
Here's the source from my project's facebook test page, containing complete implementations of a few common facebook tasks. The only thing redacted is the facebook app ID. Obviously, you don't have our libs but in this case they just verify the session and draw headers.
<html>
<head>
<meta charset="UTF-8" />
<title>phyre Lux Client</title>
<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.8.5.custom.min.js"></script>
<script type="text/javascript" src="../phyrelib-clientside.js"></script>
<link type="text/css" href="../css/custom-theme/jquery-ui-1.8.5.custom.css" rel="stylesheet">
<link type="text/css" href="../css/phyre.css" rel="stylesheet">
<script src="js/typeface.js"></script>
<script src="js/cicle_fina.typeface.js"></script>
</head>
<body onLoad='return 0;' id='luxDocBody' class='phyremodule'>
<div id='fb-root'></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script type='text/javascript'>
FB.init({appId: 'REDACTED', status: true, cookie: true, xfbml: true});
FB.Event.subscribe('auth.sessionChange', function(response) {
if (response.session) {
// A user has logged in, and a new cookie has been saved
alert('yay');
} else {
// The user has logged out, and the cookie has been cleared
alert('nay');
}
});
FB.ui(
{
method: 'stream.publish',
message: 'getting educated about Facebook Connect',
attachment: {
name: 'Connect',
caption: 'The Facebook Connect JavaScript SDK',
description: (
'A small JavaScript library that allows you to harness ' +
'the power of Facebook, bringing the user\'s identity, ' +
'social graph and distribution power to your site.'
),
href: 'http://github.com/facebook/connect-js'
},
action_links: [
{ text: 'Code', href: 'http://github.com/facebook/connect-js' }
],
user_message_prompt: 'Share your thoughts about Connect'
},
function(response) {
if (response && response.post_id) {
alert('Post was published.');
} else {
alert('Post was not published.');
}
}
);
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
alert('yay');
} else {
// no user session available, someone you dont know
alert('nay');
}
});
</script>
<fb:login-button></fb:login-button>
</body>
</html>

Create A Facebook Status Update Box for Website

I want to know how I can update my Facebook status (and allow other users to update theirs) from a website.
I found this http://developers.facebook.com/docs/reference/rest/status.set/ but couldn't figure out how to get the information from a form to the URL.
Form: USERS STATUS
https://api.facebook.com/method/status.set?status=USERS+STATUS&access_token=********************&format=json
Could anyone shed some light on this, or help me? :(
Update!
I found this piece of coding
<html>
<head>
<title>My Great Website</title>
</head>
<body>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js">
</script>
<script>
FB.init({
appId:'128083010546416', cookie:true,
status:true, xfbml:true
});
FB.ui({ method: 'feed',
message: 'Facebook for Websites is super-cool'});
</script>
</body>
</html>
So, how do I get the text from a form (or something) to the "message" box?
No, you can't fill the box in the Facebook Dialogs with the data you have. Instead you should use FB.api:
var body = 'Reading Connect JS documentation'; // Content to share
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
alert('Error occurred');
} else {
alert('Post ID: ' + response.id);
}
});