Facebook 101

Oliver Heeger
Senior Web-Entwickler & Teamleiter
denkwerk GmbH
denkwerk.com
twitter.com/echt
fb.com/oliver.heeger

Timeline

Html 5 Social Plugins

Like button

<div class="fb-like" data-href="http://oliver-heeger.de/facebook101/" data-send="false" data-layout="button_count"
data-ref="fbcamp_slides" data-locale="de_DE" data-width="450" data-show-faces="false" data-font="verdana"></div>

.fb_edge_widget_with_comment span.fb_edge_comment_widget iframe.fb_ltr {display: none !important;}                

Comment Box

<div class="fb-comments" data-href="http://oliver-heeger.de/facebook101/" data-num-posts="2" data-width="470"></div>                

Read more: http://developers.facebook.com/docs/plugins/

JavaScript SDK

To get ALL Plugins, UI, Canvas or API methods running you need to load and initialize the JavaScript SDK

<div id="fb-root">
    <!-- Required & The fb-root element must not be hidden using display: none or visibility: hidden,
    or some parts of the SDK will not work properly in Internet Explorer -->
</div>
<script>
// Facebook API loaded
window.fbAsyncInit = function() {
    FB.init({
        appId      : <?= $appId ?>, // App ID
        channelUrl : '//oliver-heeger.de/facebook101/channel.php', // Channel File
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
    });

    // Additional Facebook initialization code here
};

// Load the SDK Asynchronously
(function(d){
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/de_DE/all.js";
    ref.parentNode.insertBefore(js, ref);
}(document));
</script>                    

Read More: https://developers.facebook.com/docs/reference/javascript/

Open Graph Protocol

To provide the right information to facebook you should include the og:metatags.

<meta property="og:title" content="Facebook 101" />
<meta property="og:url" content="http://<?= $_SERVER['SERVER_NAME'] ?>/facebook101/" />
<meta property="og:image" content="http://<?= $_SERVER['SERVER_NAME'] ?>/facebook101/img/share.jpg" />
<meta property="og:type" content="website" />
<meta property="og:locale" content="de_DE" />
<meta property="og:site_name" content="Facebook 101" />
<meta property="fb:app_id" content="<?= $appId ?>" />                    

Read More: https://developers.facebook.com/docs/opengraphprotocol/

Open Graph Protocol

Direct Media embedding

<meta property="fb:app_id" content="284762368275284" />
<meta property="og:site_name" content="My Website" />
<meta property="og:title" content="Look at this Video" />
<meta property="og:type" content="video" />
<meta property="og:url" content="http://domain.com/index.php" />
<meta property="og:image" content="http://i3.ytimg.com/vi/IgB-bcsdXAw/0.jpg" />
<meta property="og:video" content="http://www.youtube.com/v/IgB-bcsdXAw?version=3&amp;autohide=1&amp;rel=0" />
<meta property="og:video:type" content="application/x-shockwave-flash" />
<meta property="og:video:width" content="640" />
<meta property="og:video:height" content="360" />
<meta property="og:description" content="Description!" />                     

Read More: https://developers.facebook.com/docs/opengraphprotocol/

Fb.ui

FB.ui is a generic helper method for triggering Dialogs that access the Facebook Dialog API endpoint.

FB.ui({method: 'feed', ...}); // Deprecated

FB.ui({method: 'share', ...});

FB.ui({method: 'stream.share', ...});

FB.ui({method: 'send', ...});

FB.ui({method: 'friends', ...});

FB.ui({method: 'apprequest', ...});

FB.ui({method: 'pay', ...});

fb-login-button ()

// Initial call on load
FB.getLoginStatus(function(response) {
    /*
    response = {
        status: 'connected',
        authResponse: {
            accessToken: '...',
            expiresIn:'...',
            signedRequest:'...',
            userID:'...'
        }
    }
    */
    if (response.status === 'connected') {
        // logged in to this page!
    } else if (response.status === 'not_authorized') {
        // Not logged in to this page!
    } else {
        // Hey, you arent even on fecebook!
    }
});
                        
<div class="fb-login-button"
data-show-faces="false" data-width="200" data-max-rows="1"
data-scope="friends_likes,publish_stream">Login</div>

// subsription to a login change!
FB.Event.subscribe('auth.login', function(response) {
  // same as left
});
                        

Log window

Read more:
Technical Guides: Login
Available permissions
App Settings on facebook
Auth SDK Reference

Fb.login ()

Login
// Initial call on load
FB.getLoginStatus(function(response) {
    /*
    response = {
        status: 'connected',
        authResponse: {
            accessToken: '...',
            expiresIn:'...',
            signedRequest:'...',
            userID:'...'
        }
    }
    */
    if (response.status === 'connected') {
        // logged in to this page!
    } else if (response.status === 'not_authorized') {
        // Not logged in to this page!
    } else {
        // Hey, you arent even on fecebook!
    }
});
                        
$('#logmein').on('click', function() {
    FB.login(function(response) {
        if (response.status == 'connected') {
            window.top.location.href = 'http://oliver-heeger.de/facebook101';
        }
    },{scope:'email'} );
    return false;
});
                        

Log window

Problem: No cookie match wit php SDK

Read more:
Login with Javascript SDK
Available permissions
App Settings on facebook
Auth SDK Reference

Please log in.

The following slides need a logged in user to use the graph API. You should log in to proceed to the next slides.

Graph API

You can use the graph API to get almost any information on facebook.

https://graph.facebook.com/4 Mark Zuckerberg
https://graph.facebook.com/4/picture Mark Zuckerberg's Picture
https://graph.facebook.com/me/feed Actual user's Timeline
https://graph.facebook.com/search?q=sushi&type=place¢er=50.945882,6.957779&distance=10000 Search Sushi Places in Cologne
https://graph.facebook.com/?ids=http://www.heise.de/ Get graph information from any Website
https://graph.facebook.com/138566186213770/members Members of the Group Facebook Developers Germany

Read More: https://developers.facebook.com/docs/reference/api/

PHP SDK

Use the php API to get information about the logged in user (/me)

require_once("php-sdk/facebook.php");
$config = array();
$config['appId'] = $appId;
$config['secret'] = $appSecret;
$facebook = new Facebook($config);
// Get the user id:
echo $facebook->getUser();
// 0

// get the /me object (if we have an user id)
if ($facebook->getUser()) {
    try {
        $me = $facebook->api('/me');
        echo $me['name'];
    } catch (FacebookApiException $e) {
        echo $e->getMessage();
    }
}
// Oops, you're not logged in!                    

graph API (php Example)

Always use try...catch if you use the php API.

if ($facebook->getUser()) {
    try {
        $liked = $facebook->api('/me/likes/116757168425');
        if (isset($liked['data'][0]['name'])) {
            echo 'great, you like ' . $liked['data'][0]['name'] . ' since ' . $liked['data'][0]['created_time'];
        } else {
            echo 'Oops, try again dude!';
        }
    } catch (FacebookApiException $e) {
        echo $e->getMessage();
    }
}

// Oops, you're not logged in!                    

graph API(JavaScript Example)

Use the js API post a status to facebook.

(if you click here you will post a Message to facebook without confirmation.

$("#fb_api_post").click(function(){

    var data=
        {
        name: "Facebook 101",
        caption: "Facebook 101 for developers",
        message: "The ultimate 101 for beginnig and advanced facebook developers.",
        link: 'http://oliver-heeger.de/facebook101/',
        picture: 'http://oliver-heeger.de/facebook101/img/share.jpg',
        description: "The ultimate 101 for beginnig and advanced facebook developers.",
        actions: [{ name: 'Go to the Facebook 101', link: 'http://oliver-heeger.de/facebook101/' }]
    };
    FB.api('/me/feed', 'post', data, function(response) {
        if (!response || response.error) {
            alert(response.error.message);
        } else {
            alert('Post ID: ' + response.id);
        }
    });
    return false;
});
                    

fql.query

Evaluates an FQL (Facebook Query Language) query.

$fql = '
    SELECT name, pic_square,music
        FROM user
        WHERE (
            uid IN (SELECT uid2 FROM friend WHERE uid1 = me())
        )
        AND music != '' order by name';
$param = array(
    'method' => 'fql.query',
    'query' => $fql,
    'callback' => ''
);
$fqlResult = $facebook->api($param);
                
Oops, you're not logged in!
Read More: http://developers.facebook.com/docs/reference/fql/

Tipps & Tricks

Add an application to a facbook Page

To add an application to a facbook Page you have to call the following url with your Parameters:
https://www.facebook.com/dialog/pagetab?app_id={APP_ID}&display=popup&next={NEXT URL}

Add this bookmarklet to your bookmarks to open a popup with the right Parameter.

javascript: var d = document,f = 'https://www.facebook.com/dialog/pagetab?app_id=',l = d.location,e = encodeURIComponent,
p = '&display=popup&next=http://www.facebook.com';1;a = function(){
var appid = l.href.match(/\d{10,30}/);if (!window.open(f + appid + p, 'linter', 'toolbar=0,status=0,resizable=1,width=400,
height=200')) l.href = f + appid + p};if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0);else {a()}
                

Drag me to your Bookmarks!

 

Debug the current Page

To debug the current page in the Facebook debugger, add this bookmarklet to your bookmarks to open a popup with the right Parameter.

javascript:var d=document,f='http://developers.facebook.com/tools/debug/',l=d.location,e=encodeURIComponent,
p='og/object?q='+e(l.href);1;a=function(){if(!window.open(f+p,'linter','toolbar=0,status=0,resizable=1,
width=1050,height=800'))l.href=f+p};if(/Firefox/.test(navigator.userAgent)) setTimeout(a,0);else{a()}
                

Drag me to your Bookmarks!

Tipps & Tricks

Remove overflow hidden & redirection in development.

If you develop apps on different staging server, you can use this snippet to enhance your debugging capatibilities.

<script type="text/javascript">
    try{
        if (window.location.hostname === 'liveserver.com' && top === self) {
            top.location.href = 'http://www.facebook.com/YOURPAGE?sk=app_YOURAPP';
        }else if(top === self){
            document.getElementsByTagName("body")[0].style.overflow = 'visible';
        }
    }catch(e){}
</script>                

This redirects direct calls to your app to your Facebook page and removes the overflow:hidden if you are developing.

Jobs

We are currently looking for:

Frontend-Entwickler (m/w) Köln
TYPO3 –Integrator (m/w) – Freelancer (Darmstadt)Darmstadt
JavaScript-Entwickler (m/w)Köln
Senior Java Entwickler (m/w)Köln
Web-Entwickler (m/w) – Schwerpunkt PHP/JavaScriptKöln
TYPO3-Extension-Entwickler (m/w) – Festanstellung und FreelancerDarmstadt, Köln
TYPO3-Entwickler (m/w)-Festanstellung und FreelancerDarmstadt, Köln
Software Entwickler/in (PHP)Köln
Senior Software-Entwickler (m/w)Berlin, Köln
PHP-Entwickler (m/w)Köln
www.denkwerk.com/category/de/jobs/