I have been tasked with rebuilding an application (CakePHP 2.0, php 5.6) that receives a request, reformats/maps the body to API specific fields and makes requests to multiple APIs with the newly mapped body.
Once the responses are coming back they will be decoded and placed in the output array as a response from the application.
Currently the decoding (mapping from API specific fields) process happens in sequence once the Multicurl requests return.
My idea is to process the responses and soon as they arrive, and I am attempting to do so in parallel.
One complexity is that every target API needs 4 very specific mapping functions so every API object has a map and reverse map for 2 different operations.
A client requirement is to have the minimum number of dependencies, the solution should preferably be in raw php, no libraries wanted. The KISS solution has been requested.
I have considered the following approaches but they all have drawbacks.
Multicurl waits for the slowest response to return all responses. This is the current approach, no parallel response processing.
pthreads not compatible with Apache, command line only.
Can't pass complex objects (API object) via Sockets easily.
Too many dependencies and/or too immature. a) Appserver b) Kraken c) RabbitMQ d) socket.io
I am looking for PHP 7 (nothing else) alternatives to this task.
Any suggestions?
It's worth noting that 'parallel' and 'asynchronous' are separate concepts.
eg: ReactPHP and it's ilk [node.js included] are asynchronous, but still single-threaded, relying on event loops, callbacks, and coroutines to allow out-of-order execution of code.
Responding to your assessment of approaches:
curl_multi()
.pthreads
is incompatible with apache, it's that it's incompatible withmod_php
.serialize()
is one option.json_encode()
is going to be the way to go.