How to: Asynchronous PHP

Over the Christmas period I had a play around with getting PHP to run code asynchronously (not as easy as it sounds). When you have code, like API calls, which take anywhere near to a second to complete it drastically adds to the loading times of your pages.

One easy solution for web apps is to only call the back-end PHP using AJAX. This means the page doesn’t hang and reload, things can happen concurrently and quickly. This solution isn’t always available though or simple (when working with legacy code for one).

What I came up with took inspiration from various different blogs. They were all having the same problem, but instead of just taking this code and implementing it, I wanted to make it easier for people to do. So I wrote a library (or a micro-library) which you can include and use with Composer to help run code concurrently.

Limitations: It only working on unix systems at the moment, due to how it’s built around the filesystem.

View Project Site View on Github

Show me the Code!#

The library is designed to be very simple to interface with, and so can just be called like so:

$async = new Async();

To Install:#

Simply add the library to your Composer.json file:

"require": {
   "eddturtle/php-async-calls": "dev-master"

Also, remember to run sudo composer update and include your Composer autoload file in your code – then you should be good to go.

View Project Site View on Github

Using it? Tested it?#

Let me know how you get on in the comments, or if you have any questions.