Learning the Facebook PHP API, What they don’t tell you

I finally had a use for a Facebook app so I decided to learn how to code one. My education started with some Lynda.com video lessons, one on the PHP API and one on the JavaScript API. They are related courses and I did the PHP one first, then the JavaScript one. They should be done in the other order. The JavaScript one contains some background information that is useful. In the end, the PHP one is quite out of date, as the current version has made much of the information obsolete. If you have access and the time, I’d still go through it as the stuff about the Graph API structure is still quite relevant, just don’t expect to be able to follow along with the code samples.

The new API has quite a different structure from the old one. It is written in an object-oriented fashion and has been expanded and refined greatly. Security has changed quite a bit as well. All this is fine and good, and there is plenty of getting started type tutorials around, but unfortunately, not all the documentation is easily accessible and many things are undocumented. In this article I will document the issues I ran into and how I got around them.

If you aren’t using Composer, read the Github page

Facebook recommends using Composer to download and install the SDK and to generate an autoloader script to more easily handle inclusion of necessary files into your PHP scripts. I do not use Composer, so I had to use the manual steps. These steps are shown on the Github site, not on Facebook’s getting started page. So first thing you need to know, if you aren’s using Composer, go to this link on Github to see how to include files.

The short version of this is once you download the API, put the whole directory on your server, not just the src/Facebook directory. There is an autoloader script you will want. Then, you will add these two lines to your PHP script:


define('FACEBOOK_SDK_V4_SRC_DIR'. 'path/to/sdk');
require __DIR__ . '/path/to/sdk/autoload.php';

Now, nowhere did I find how these paths were to be defined. The second one, but virtue of the use if __DIR__ is a fully qualified path to the autoload file in the sdk. That can be reasoned easily enough. The first one, however, is not as well defined. I found that for the first one, you can use either a fully qualified path, or a relative path if you leave off the leading /. For example, if your files are in /home/myproject/public_html/facebook_app, and the sdk you downloaded was the same one I did and you left it labeled the same (facebook-php-sdk-v4-4.0-dev), then they can look like this:


define('FACEBOOK_SDK_V4_SRC_DIR'. '/home/myproject/public_html/facebook_app/facebook-php-sdk-v4-4.0-dev/src/Facebook');
require __DIR__ . '/facebook-php-sdk-v4-4.0-dev/autoload.php';

or


define('FACEBOOK_SDK_V4_SRC_DIR'. 'facebook-php-sdk-v4-4.0-dev/src/Facebook');
require __DIR__ . '/facebook-php-sdk-v4-4.0-dev/autoload.php';

Facebook needs more than 128M

Once the path to the autoloader was sorted out, I got memory errors in PHP. Nowhere could I find a guideline on how much memory the API requires. I realize that memory usage will be specific to whatever you are doing with your application, but a minimum needed for example exercises would be nice to know. In my case, I set memory_limit to 256M and it worked for me.

You will need to set a timezone

While you are in the php.ini altering the memory_limit, set a date.timezone to something useful to you. I got an error when trying to get a session until this was done. This list of supported time zones may come in handy for you.

Sessions come from different places when your app is in a canvas vs direct from your site

Something to note as you are developing is that Facebook treats sessions differently if you are viewing the app within a Facebook canvas. In may places it is noted that a Facebook app is just a page on your server shown thought the Facebook wrapper. This is mostly still true, but if you need to get a session to do actual requests, the process will be different in the canvas context than on your page direct from your site.

The easy way to see if your app is being viewed within a canvas it to check the request for a signed_request parameter. Facebook will send that parameter when you are within a Facebook canvas.

FacebookSession documentation does exist

Speaking of the Facebook session object, there is official documentation for it even though it isn’t linked anywhere on the Facebook developer site. I don’t know why, but in most places the various objects are linked to their documentation pages as you’d expect. The FacebookSession object however is not linked, just colored like a link. Anyway, the documentation exists and here it is.

Ok, so that’s all I have for now. If I find any other useful nuggets I’ll post them here on this site. Happy coding!

Published by

Dan

Been a programmer, primarily in the internet space, since late 1997. Google+ Profile

One thought on “Learning the Facebook PHP API, What they don’t tell you”

  1. Another thing I just found related to having an app work as a canvas and as a website app. The redirect url you send and that you set in your app setting need to be fully qualified, and include the file name. If you are relying on index.xxx, you must include it in the settings and in the redirect.

Leave a Reply

Your email address will not be published. Required fields are marked *