PHP FB API
PHP FB API
Provides access to the Facebook Platform.
Version 2.0.3 ‐ 11‐5‐2010
http://github.com/facebook/php‐sdk/ public static $CURL_OPTS public function __construct($config) protected function _restserver($params)
// Array ‐ Default options for curl.
Default options for curl. // Initialize a Facebook Application.
pp // Invoke the old restserver.php endpoint.
p p p
public function getSession() protected function getUrl($name, $path='',
// Get the session object. This will automatically look for a signed
example.php // session sent via the Cookie or Query Parameters if needed. $params=array())
<?php <!doctype html> appId : '<?php echo $facebook‐>getAppId(); ?>', button></fb:login‐button> // Build the URL for given domain alias, path and parameters.
require '../src/facebook.php'; <html status : true, // check login status </div>
xmlns:fb="http://www.facebook.com/2008/fbml"> cookie : true, // enable cookies to allow the server to <div> public function getUser()
// Create our Application instance.
$facebook = new Facebook(array(
<head>
<title>php‐sdk</title>
access the session
xfbml : true // parse XFBML
Without using JavaScript & XFBML:
<a href="<?php echo $loginUrl; ?>"> // Get the UID from the session. protected function getCurrentUrl()
'appId'
appId => '254752073152',
254752073152 , }); <img
img // Returns the Current URL, stripping it of known FB parameters that
// Returns the Current URL, stripping it of known FB parameters that
'secret' => '904270b68a2cc3d54485323652da4d14', <style> src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/
// should not persist.
'cookie' => true,
));
body {
font‐family: 'Lucida Grande', Verdana, Arial, sans‐
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
4li2k73z.gif">
</a>
public function getLoginUrl($params=array())
serif; window.location.reload(); </div> // Get a Login URL for use with redirects. By default, full page
$me = null; } }); <?php endif ?> protected static function
// Session based API call. h1 a { }; <h3>Session</h3>
// redirect is assumed. If you are using the generated URL with a
if ($session) { text‐decoration: none; <?php if ($me): ?> // window.open() call in JavaScript, you can pass in display=popup as generateSignature($params, $secret)
try { color: #3b5998; (function() { <pre><?php print_r($session); ?></pre>
$uid = $facebook‐>getUser(); } var e = document.createElement('script');
// part of the $params. // Generate a signature for the given params and secret.
$me = $facebook‐>api('/me'); h1 a:hover { e.src = document.location.protocol + <h3>You</h3>
} catch (FacebookApiException $e) { text‐decoration: underline; '//connect.facebook.net/en_US/all.js'; <img src="https://graph.facebook.com/<?php echo
error_log($e); } e.async = true; $uid; ?>/picture"> public function getLogoutUrl($params=array())
} </style> document.getElementById('fb‐root').appendChild(e); <?php echo $me['name']; ?>
} }()); // Get a Logout URL suitable for use with redirects.
</head> </script> <h3>Your User Object</h3>
// login or logout url
// g g will be needed depending on
p g <body> y <pre><?php
p p pp print_r($me); ?></pre>
($ ); /p
current user state. <h1><a href="example.php">php‐sdk</a></h1> <?php else: ?> public function
bl f
if ($me) {
$logoutUrl = $facebook‐>getLogoutUrl();
<!‐‐
We use the JS SDK to provide a richer user <?php if ($me): ?>
<strong><em>You are not
Connected.</em></strong> getLoginStatusUrl($params=array()) Cheat Sheet available at
} else { experience. For more info, <a href="<?php echo $logoutUrl; ?>"> <?php endif ?>
}
$loginUrl = $facebook‐>getLoginUrl(); look here: http://github.com/facebook/connect‐js
‐‐>
<img
src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/ <h3>Naitik</h3>
// Get a login status URL to fetch the status from facebook. www.princessdesign.net
cxrz4k7j.gif"> <img
// This call will always work since we are fetching
public data.
<div id="fb‐root"></div>
<script>
</a>
<?php else: ?>
src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
public function api(/* polymorphic */)
$naitik = $facebook‐>api('/naitik'); window.fbAsyncInit = function() { <div> </body> // Make an API call.
?> FB.init({ Using JavaScript & XFBML: <fb:login‐ </html>