简体   繁体   English

在curl / PHP中保持活动

[英]Keep-alive in curl / php

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). 我正在用PHP编写网关脚本,该脚本连接到远程服务器,获取一些信息,并将其返回以供JSON使用(不支持JSONP)。

This gateway is being requested every second, so it's very important for curl to use keep-alive. 每秒都会请求此网关,因此curl使用keep-alive非常重要。 From what I learned, curl will do it automatically if we will use the same handle across multiple requests. 从我学到的知识来看,如果我们在多个请求中使用相同的句柄, curl将自动执行此操作。

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? 或者,也许还有其他方法可以确保保持活动状态?

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. 使用具有内容自适应功能的代理(Squid和Greasyspoon可以在此处使用),这确实需要进行一些设置。 But you will be able to write scripts in java, javascript or ruby to adapt your content. 但是您将能够使用Java,JavaScript或ruby编写脚本来适应您的内容。

  2. Run your PHP script as a deamon, sort of like a webserver. 将PHP脚本作为守护程序运行,就像运行Web服务器一样。 This would take a bit of engineering, but it can be done with PHP. 这将需要一些工程,但是可以使用PHP来完成。 You would be getting into sockets and threading. 您将进入套接字和线程。

You might be able to use this as a starting point: http://nanoweb.si.kz/ 您可能可以以此为起点: http : //nanoweb.si.kz/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM