Keep-alive in curl / php

2019-06-03 17:57发布

I'm writing a gateway script in PHP which connects to a remote server, obtains some information and returns it for JSON usage (no JSONP possibility).

This gateway is being requested every second, so it's very important for curl to use keep-alive. From what I learned, curl will do it automatically if we will use the same handle across multiple requests.

The question is: how do I store the handle between two reloads? It's not possible to store the handle resource in session, it also can't be serialized.

Or maybe there's other way to ensure keep-alive in curl?

1条回答
淡お忘
2楼-- · 2019-06-03 18:33

Generally speaking, every request exists independent of every other request. Connections and other resources are not pooled between requests.

There are possible solutions

  1. Use a proxy with content adaptation (Squid and Greasyspoon would work here) this does take some work to set up. But you will be able to write scripts in java, javascript or ruby to adapt your content.

  2. Run your PHP script as a deamon, sort of like a webserver. This would take a bit of engineering, but it can be done with PHP. You would be getting into sockets and threading.

You might be able to use this as a starting point: http://nanoweb.si.kz/

查看更多
登录 后发表回答