我正在为wordpress编写一个插件,该插件需要针对用户的每个请求调用一个API.
I'm writing a plugin for wordpress that needs to call an API for every request the user makes.
这些API调用是使用HTTPS协议完成的.当前,对于每个新用户请求,我都需要重新打开HTTPS连接.
These API-calls are done using the HTTPS protocol. Currently, for every new user request, I need to reopen the HTTPS connection.
是的,curl允许持久连接(重用手柄或使用多手柄),但我想在多个用户请求中保持连接.
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.
那么:是否可以通过多个PHP进程保持HTTPS连接打开并重用它?另一种选择是让用户的浏览器进行API对话.但是,如果有可能,我想避免这种情况.
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.
推荐答案我最终通过浏览器发出了这些请求.当服务器告诉浏览器时,浏览器使HTTP(S)连接保持打开状态.
I ended up making these requests through the browser. Browsers keep HTTP(S) connections open when the server tells them to.
A,此解决方案存在一些缺点:
Alas, this solution entails some disadvantages:
- 认证比较困难
- 由于必须维护更多连接,服务器上的负载更多
- 该解决方案需要其他JavaScript
但是请求要快得多(大约3倍),并且运行WordPress的服务器上的负载被最小化了.
But requests are much faster (about 3x) and load on the server where WordPress is running is minimized.
更多推荐
PHP:在多个请求中保持与API的HTTPS连接打开
发布评论