Sunday, February 9, 2014

How to create a Facebook application using PHP

If you are searching for something like “how to create a facebook application using Php” then you are at the right place. Its as easy as making tea ;)
Through this code you can create your own gaming facebook application, contest facebook application etc. Many facebook users use many facebook applications and wish to create his/her own facebook application. In fact they have many new and unique ideas but they don’t know how to implement it.
So here are the simple steps to write a facebook application using Php
Step 1. you need to download the Facebook PHP SDK (software development Kit) which will allow you to create a facebook canvas and communicate with the facebook by using API.

Please download it from here facebook-php-sdk.zip.
Step 2. Upload it to your server and extract folder there.
Step 3. Now create a new php page for example index.php
Write the following code for a simple facebook application:-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
// include the SDK
require_once 'facebook-php-sdk/src/facebook.php';
 
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'your application ID',
'secret' => 'Your secreate key',
'cookie' => true,
));
 
//Save the user session if exist
$session = $facebook->getSession();
 
$me = null;
// Session based API call.
//check session
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
 
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
header("Location: ". $loginUrl );
die();
 
}
 
// This call will always work since we are fetching public data.
 
$ankur= $facebook->api('/ankursharma.net');
 
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Test Application - 7tech.co.in</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId   : '<?php echo $facebook->getAppId(); ?>',
session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
status  : true, // check login status
cookie  : true, // enable cookies to allow the server to access the session
xfbml   : true // parse XFBML
});
 
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
 
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<h3>Ankur</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $ankur['name']; ?>
 
<div>This Application is Developed by <a href="http://www.ankursharma.net" target="_blank">Ankur Sharma - www.ankursharma.net</a></div>
</body>
</html>
Step 4. Now create a new application on facebook from here
Step5. Enter your Application ID and Secret key on the page index.php
That’s it, your application is ready just enter the php code you want to use.
Have Fun…

No comments:

Post a Comment