Home arrow PHP arrow Implement Facebook Javascript SDK with PHP

Implement Facebook Javascript SDK with PHP

In this programming tutorial you will learn how to implement the Facebook Javascript SDK in an HTTPS environment with a little bit of help from PHP.

Author Info:
By: Codex-M
Rating: 5 stars5 stars5 stars5 stars5 stars / 27
July 11, 2011
  1. · Implement Facebook Javascript SDK with PHP
  2. · Getting Facebook Like Button to Work in HTTPS
  3. · Making Facebook Comments and Like Box Work in HTTPS Mode

print this article

Implement Facebook Javascript SDK with PHP
(Page 1 of 3 )

Websites using SSL (https) for some (or all) of their pages seem to be having issues with the implementation of Facebook social plug-ins such as the Like button, comments, Like Box etc. Since the simplest and straightforward implementation of the Facebook Like button uses the iFrame method, visitors to the website will often receive “mixed content warnings” if they are visiting secure pages (https) containing insecure elements.

To make the Facebook social plug-ins completely compatible with secure (https) pages, you should be using the JavaScript SDK on your website.  However implementing this library is not always straightforward and there are things that you need to know before doing the implementation.

Get an Application ID

The very first step is to get an application ID for your Facebook JavaScript SDK implementation. Follow the steps below:

  • Go to this page: http://www.facebook.com/developers/createapp.php
  • You need to have a verified Facebook account to login.
  • If it asks permission, click “Allow”.
  • Click “Setup New App”.
  • Enter some information about your App. Make sure you fill the following fields (recommended):

a.) Application Name.
b.) URL for the Terms of Service
c.) URL for the Privacy Policy
d.) Contact email Address
d.) Description of your Application

  • Hit “Save Changes”.
  • You will then be provided with an App ID (Application ID), App Secret, etc.
  • Click “Website”, under core settings, assign a site URL and site Domain. An example value is:

site URL: http://www.yourdomain.com/
site Domain: yourdomain.com

  • Click “Save Changes”.

Simplest PHP JavaScript SDK Example in HTTPS

Now that you have an App ID and Secret, lets try creating a PHP page that will run the JavaScript SDK in an HTTPS environment. Below is the basic PHP script that contains all the required JavaScript SDK components:


//Define app ID and secret- REQUIRED


//Function to get Facebook cookie -REQUIRED

function get_facebook_cookie($app_id, $application_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\"'), $args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
if (md5($payload . $application_secret) != $args['sig']) {
return null;
return $args;
$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
<!DOCTYPE html>

<!-- Declare XML namespace attribute to Facebook, so that XFBML tags will render in Internet Explorer-REQUIRED -->

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="https://www.facebook.com/2008/fbml">

<!-- Implement all Facebook social plugins here that depends on JavaScript SDK- REQUIRED -->

This is the basic PHP page that contains all the required JavaScript SDK components for implementation.

<!-- Load SDK-REQUIRED -->
<!-- Since JavaScript SDK should work in SSL pages, it should be using https://connect.facebook.net/en_US/all.js and NOT its http version-->

<div id="fb-root"></div>
<script src="https://connect.facebook.net/en_US/all.js"></script>
FB.init({appId: '<?= FACEBOOK_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {

blog comments powered by Disqus

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials