Facebook PHP SDK Zend Framework 2

In ZF1 I would generally include 3rd party API wrappers (Facebook, MailChimp, Google, etc) in Bootstrap.php using something like:

1
2
3
4
5
6
7
8
9
protected function _initIncludes(){
include_once( 'MobileDetect/Mobile-Detect-2.php' );
include_once( 'Facebook/php-sdk-v3.1.1-27/src/facebook.php' );
include_once( 'Google/google-api-php-client-0.5.0/src/apiClient.php' );
include_once( 'MailChimp/MCAPI-1.3.class.php' );
} //_initIncludes

Facebook PHP SDK Zend Framework 2

However there isn’t really anywhere appropriate to ‘include’ things this way in ZF2. Ideally we want to include the 3rd party class in the (/all) module(s) that actually use it, but without trying to include it more than once.

The best way I’ve found of doing this is to add the 3rd party class into Zend’s Class Map Autoloader like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public function getAutoloaderConfig() {
return array(
'ZendLoaderClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
array(
'Facebook' => 'vendor/facebook/php-sdk/src/facebook.php',
),
),
'ZendLoaderStandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}

This shows how I’m including the ‘Facebook’ class. Remember that ZF2 chdir()’s to the application’s root directory, so the path is relative to that. I can then just create a Facebook instance with:

1
2
3
4
5
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));

Adding things via this method has a few advantages:

  1. The class is included lazily.
  2. The same entry can be included in multiple modules without there being a risk of conflict.
  3. Zend’s Class Map Autoloader is very, very, very fast.
Adding my “3rd party” module into /vendor in Zend Framework

This wasn’t initially obvious to me, but when creating a generic module to use across multiple Zend Framework 2 projects (thus putting it in /vendor) you can include a Module.php file in the root of the module’s directory to make registering the namespace with Zend’s StandardAutoloader simple. (I had assumed this was just how it worked under the /module directory).

So for example – I have a module with the namespace ‘Nas’, so I store it under:
/zf2/vendor/Nas

And then I have Module.php here:
/zf2/vendor/Nas/Module.php
containing…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
namespace Nas;
class Module
{
public function getAutoloaderConfig()
{
return array(
'ZendLoaderStandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__,
),
),
);
}
}

Finally I needed to add the module into /zf2/config/application.config.php:

1
2
3
4
5
6
'modules' => array(
'Application',
'Album',
'Nas',
),

And that’s it. After that I can start accessing classes under my ‘Nas’ namespace from anywhere within my ZF2 app.

Latency between Amazon Web Services Regions

Recently I’ve had to spend quite a bit of time working out which of the 5 Amazon regions is best positioned for various services I’ve been setting up. As part of this I measured the latency between each of the Amazon regions. For anyone who’s interested – here are the results.

Note: For ease of reading I’ve including all measurements under each region. Half of the data will therefore be duplicated as I’ve assumed (rightly or wrongly) that EU-West -> US-East is the same as US-East -> EU-West (for example).

From US-West to…

  • US-East: 82 ms
  • AP-North: 130 ms
  • EU-West: 155 ms
  • AP-South: 185 ms

From US-East to…

  • US-West: 82 ms
  • EU-West: 101 ms
  • AP-North: 199 ms
  • AP-South: 256 ms

From EU-West to…

  • US-East: 101 ms
  • US-West: 155 ms
  • AP-North: 282 ms
  • AP-South: 292 ms

From AP-North to…

  • AP-South: 84 ms
  • US-West: 130 ms
  • US-East: 199 ms
  • EU-West: 282 ms

From AP-South to…

  • AP-North: 84 ms
  • US-West: 185 ms
  • US-East: 256 ms
  • EU-West: 292 ms

These numbers represent the rounded average ping time over 30 measurements between two EC2 instances, one in each region. They were all performed on the 8th June 2011.

I’ve put them here for curiosity value only. I strongly recommend you perform your own tests if you plan on making any decisions based on the outcome.

Newsletter Subscribe

Get the Latest Posts & Articles in Your Email

We Promise Not to Send Spam:)