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

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

问题描述

我在php 5.3.0中做了一些cURL工作。



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



Theres这个长期以来接受的方法是让它们进入/退出请求:

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

但我碰到一些情况,我需要运行脚本的多个副本相同的目录,他们相互对彼此的cookie文件。是的,我知道我可以使用tempnam(),并确保每次运行都有自己的cookie文件,但这导致我的第二个问题。



还有问题的这些cookie文件在磁盘上。磁盘I / O缓慢,瓶颈我确定。我不想在脚本完成时处理清理cookie文件(如果它甚至以一种能让我清理它的方式退出)。



任何想法?

解决方案

您可以使用 CURLOPT_COOKIEJAR Linux / MacOS X 或 NULL 将文件设置为 / dev / null / code>用于 Windows 。这将阻止cookie写入磁盘,但它会保持它们在内存中,只要重复使用句柄,并且不调用 curl_easy_cleanup()


I'm doing some cURL work in php 5.3.0.

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

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.

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?

解决方案

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天全站免登陆