在curl/PHP中保持活动 [英] Keep-alive in curl / php

查看:108
本文介绍了在curl/PHP中保持活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

每秒都会请求此网关,因此curl使用keep-alive非常重要.据我了解,如果我们在多个请求中使用相同的句柄,curl将自动执行该操作.

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.

问题是:如何在两次重装之间存储手柄?无法将handle资源存储在会话中,也无法序列化.

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.

有可能的解决方案

  1. 使用具有内容自适应功能的代理(Squid和Greasyspoon可以在此处使用),这确实需要进行一些设置.但是您将能够使用Java,JavaScript或ruby编写脚本来适应您的内容.

  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.

将您的PHP脚本作为守护程序运行,就像Web服务器一样.这将需要一些工程,但是可以使用PHP来完成.您将进入套接字和线程.

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.

您也许可以以此为起点: http://nanoweb.si.kz/

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

这篇关于在curl/PHP中保持活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆