任何将 curl 的 cookie 保存在内存中而不是磁盘上的方法 [英] Any way to keep curl's cookies in memory and not on disk

查看:49
本文介绍了任何将 curl 的 cookie 保存在内存中而不是磁盘上的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 php 5.3.0 中做一些 cURL 工作.

I'm doing some cURL work in php 5.3.0.

我想知道是否有任何方法可以告诉 curl 句柄/对象将 cookie 保存在内存中(假设我对多个请求重复使用相同的句柄),或者以某种方式返回它们并让我将它们传回去制作新手柄时.

I'm wondering if there is any way to tell the curl handle/object to keep the cookies in memory (assuming I'm reusing the same handle for multiple requests), or to somehow return them and let me pass them back when making a new handle.

有一种长期被接受的方法可以让他们进入/退出请求:

Theres this long accepted method for getting them in/out of the request:

curl_setopt($ch, CURLOPT_COOKIEJAR, $filename); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $filename);

但是我遇到了一些场景,我需要从同一目录中运行脚本的多个副本,并且它们会相互踩踏 cookie 文件.是的,我知道我可以使用 tempnam() 并确保每次运行都有自己的 cookie 文件,但这导致我遇到第二个问题.

But I'm hitting some scenarios where I need to be running multiple copies of a script out of the same directory, and they step on each others cookie files. Yes, I know I could use tempnam() and make sure each run has its own cookie file, but that leads me to my 2nd issue.

也存在将这些 cookie 文件放在磁盘上的问题.磁盘 I/O 很慢,我敢肯定这是一个瓶颈.我不想在脚本完成后清理 cookie 文件(如果它甚至以一种让我清理它的方式退出).

There is also the issue of having these cookie files on the disk at all. Disk I/O is slow and a bottle neck I'm sure. I dont want to have to deal with cleaning up the cookie file when the script is finished (if it even exits in a way that lets me clean it up).

有什么想法吗?还是事情就是这样?

Any ideas? Or is this just the way things are?

推荐答案

您可以使用 CURLOPT_COOKIEJAR 选项,并将文件设置为/dev/null"Linux/MacOS XWindowsNULL".这将阻止 cookie 被写入磁盘,但只要您重用句柄并且不调用 curl_easy_cleanup(),它就会将它们保留在内存中.

You can use the CURLOPT_COOKIEJAR option, and set the file to "/dev/null" for Linux / MacOS X or "NULL" for Windows. This will prevent the cookies from being written to disk, but it will keep them around in memory as long as you reuse the handle and don't call curl_easy_cleanup().

这篇关于任何将 curl 的 cookie 保存在内存中而不是磁盘上的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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