While developing a Facebook app I caught up with a problem – the ‘CurlException of Facebook app’. The reason why I quoted the phrase is because this is a very interesting exception that’s very much difficult to understand till one sees the exact error message.

Here is how the error message looks like,

The error doesn’t comes up every time an app is run. In most cases, it shows up once in a while. That’s what happened to me. The app sometimes ran fine while sometimes it didn’t.

Being an unknown problem, I looked at various sources and found that this is one of the common problems that developers face while building apps.

Why it occurs?

Curl php extension must be installed in your web server. If this is absent you’ll see the exception.

Next and probably the biggest reason could be the delay in loading your app. Usually an app should load/respond within 10 seconds otherwise the exception is thrown by Facebook’s php SDK.

You can also see this exception when your curl functions are missing or working incorrectly. Facebook’s php SDK checks the presence of curl_init() and many other functions, and displays exception if they’re absent or working faultily.

Prevent exception from occurring

Set the value of CURL_CONNECTTIMEOUT to 40-50 or more. This will allow your app to load within an extended time period. But you should know that this might run your app but this isn’t good from user perspective as they don’t like waiting such a long time for an app.

Another way is to keep your app lightweight. If you’re using JavaScript SDK then you’re loading an excess 170kb every-time your app runs. So avoid using the JavaScript SDK for HTTP POST/GET requests, rather use Ajax calls (in JavaScript or jQuery) for calling Graph API and FQL queries.

And perhaps the best solution is to go for a better web server. Faster and responsive server makes sure your app files are loaded within time (10secs).