How to Make Async Requests in PHP
segment.io
segment.io
Or, use React (http://reactphp.org/) a PHP async library.
We're mainly trying to make the setup process really simple - no need to start up extra job servers or worker computers. Ideally they can add in our small bits of code and then start tracking analytics data without much configuration.
Are you guys in San Francisco?
Although in this case, I imagine the real problem is portability. Curl is available pretty much everywhere. exec (or more directly, pcntl/posix extensions) aren't in any out-of-the-box installation, and anything that needs to be further daemonized to get up and running (such as gearmand and gearman-manager) are even harder to use in a one-click solution.
If you use their API even just one time per request, be aware that they will fork, execute a shell, fork again and then execute curl. Imagine where this is going when you use multiple API calls per request.
As this is "just" about analytics, why not use a UDP packet or two? Sure - they might not get delivered, but is that really so bad for analytics? Sending out a UDP package is very fast and there will be no waiting going on at all.
I doubt you'd lose much more than 20ms. That's still way better than forking tons and tons of curl processes and shells.
We actually queue calls to our API and send only a single request to our servers. Even if you make multiple API calls over the course of a request, there will still be one fork per request.
I'd actually wanted to include UDP, PHP Extensions, and persistent sockets as part of the article, they just ended being a little out of scope. We might try and support them in the future, UDP is definitely an interesting idea for analytics applications.
We may also end up writing a custom in-memory queue which uses a persistent socket. For a vanilla PHP install, these seemed like the best available options.
I suspect that you'll frequently need to fallback to the socket approach. I imagine a lot of shared hosts block the capability to run exec (Godaddy, Hostgator, other common php hosts), and I believe that's where a lot of PHP lives.
We decided to inject Javascript calls and use analytics.js's client-side tracking, because it's a bit more friendly for WordPress people we think, and it keeps their personal blogs and smaller projects on the free plan.