Zend Framework 2 – How to use Facebook PHP SDK

zend-frameworkWorking on a Facebook application with Zend Framework 2 for my client, I experienced the issue with loading 3rd party libraries, in this case the facebook php sdk. I thought it would be easy, just use required_once, include on any similar syntax and everything would be fine. It wasn’t so easy, actually.

Here is how I solved the problem that could be used for any other 3rd party libraries in general. The trick is to autoload the FB SDK with ClassMapAutoloader.

How to use Facebook PHP SDK with Zend Framework 2

1) Download the facebook PHP SDK and extract files from facebook-php-sdk/src/ into /vendor folder, e.g. /vendor/FB/

2) Create file autoload_classmap.php if it doesn’t exist in the Application module (or your module) and add the following code:

return array(
	'Facebook' => 'vendor/FB/facebook.php',

3) In function getAutoloaderConfig of Module.php file add ClassMapAutoloader section if it doesn’t exist in order to load configurations from autoload_classmap.php file. The function should look as it follows:

public function getAutoloaderConfig()
	return array(
		'Zend\Loader\ClassMapAutoloader' => array(
			__DIR__ . '/autoload_classmap.php',

		'Zend\Loader\StandardAutoloader' => array(
			'namespaces' => array(
			    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,


4) After the changes you can use the facebook library in your controller:

$facebook = new \Facebook(array(
	'appId'  => 'xxx',
	'secret' => 'xxx',


Share This Post

Related Articles

One Response to “Zend Framework 2 – How to use Facebook PHP SDK”

  1. ramon says:


    Do you know how to include Facebook SDK version 4?

Leave a Reply

© 2024 LoneShooter.com. All rights reserved. Site Admin · Entries RSS · Comments RSS