Search code examples
phpwordpresscurlhttpskeep-alive

PHP: Keep HTTPS Connection to API open throughout multiple requests


I'm writing a plugin for wordpress that needs to call an API for every request the user makes.

These API-calls are done using the HTTPS protocol. Currently, for every new user request, I need to reopen the HTTPS connection.

Yes, curl allows persistent connections (reusing the handle or using the multi handle) but I would like to persist the connection throughout multiple user requests.

So: Is it possible to keep a HTTPS connection open throught multiple PHP processes and reuse it? The alternative would be to let the user's browser to the API-talk. But if it is possible I would like to avoid that.


Solution

  • I ended up making these requests through the browser. Browsers keep HTTP(S) connections open when the server tells them to.

    Alas, this solution entails some disadvantages:

    • authentication is more difficult
    • more load on the server as more connections have to be maintained
    • the solution needs additional JavaScript

    But requests are much faster (about 3x) and load on the server where WordPress is running is minimized.