有什么方法可以将curl的cookie保存在内存中而不是磁盘上

发布于 2024-08-06 04:26:27 字数 529 浏览 6 评论 0原文

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

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

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

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

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

还有一个问题是这些 cookie 文件是否存在于磁盘上。我确信磁盘 I/O 很慢并且是瓶颈。我不想在脚本完成时处理清理 cookie 文件的问题(如果它甚至以允许我清理它的方式退出)。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

云柯 2024-08-13 04:26:27

不幸的是,我认为您不能使用“php://memory”作为输入和输出流。解决方法是自己解析标头。这可以很容易地完成。下面是一个页面发出两个请求并自行传递 cookie 的示例。

curl.php:

<?php

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost/test.php?message=Hello!');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, true);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

$data = curl_exec($curl);
curl_close($curl);

preg_match_all('|Set-Cookie: (.*);|U', $data, $matches);   
$cookies = implode('; ', $matches[1]);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost/test.php');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, true);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_COOKIE, $cookies);

$data = curl_exec($curl);
echo $data;

?>

test.php:

<?php
session_start();
if(isset($_SESSION['message'])) {
    echo $_SESSION['message'];
} else {
    echo 'No message in session';
}

if(isset($_GET['message'])) {
    $_SESSION['message'] = $_GET['message'];
}
?>

这将输出“你好!”关于第二个请求。

Unfortunately, I don't think you can use 'php://memory' as the input and output stream. The workaround is to parse the headers yourself. This can be done pretty easily. Here is an example of a page making two requests and passing the cookies yourself.

curl.php:

<?php

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost/test.php?message=Hello!');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, true);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

$data = curl_exec($curl);
curl_close($curl);

preg_match_all('|Set-Cookie: (.*);|U', $data, $matches);   
$cookies = implode('; ', $matches[1]);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://localhost/test.php');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, true);  
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_COOKIE, $cookies);

$data = curl_exec($curl);
echo $data;

?>

test.php:

<?php
session_start();
if(isset($_SESSION['message'])) {
    echo $_SESSION['message'];
} else {
    echo 'No message in session';
}

if(isset($_GET['message'])) {
    $_SESSION['message'] = $_GET['message'];
}
?>

This will output 'Hello!' on the second request.

睡美人的小仙女 2024-08-13 04:26:27

您可以使用 CURLOPT_COOKIEJAR 选项,并将文件设置为“/dev/null”(对于 Linux / MacOS X)或“NULL””适用于Windows。这将阻止 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().

红墙和绿瓦 2024-08-13 04:26:27

只需将 CURLOPT_COOKIEFILE 设置为不存在的文件即可,通常空字符串是最佳选择。然后不要设置 CURLOPT_COOKIEJAR,这就是技巧。这将阻止写入文件,但 cookie 将保留在内存中。我刚刚对此进行了测试,它有效(我的测试:将 http 身份验证数据发送到 URL,该 URL 将您重定向到对请求进行身份验证的登录 URL,然后使用 cookie 将您重定向回原始 URL)。

Just set CURLOPT_COOKIEFILE to a file that doesn't exist, usually an empty string is the best option. Then DON'T set CURLOPT_COOKIEJAR, this is the trick. This will prevent a file from being written but the cookies will stay in memory. I just tested this and it works (my test: send http auth data to a URL that redirects you to a login URL that authenticates the request, then redirects you back to the original URL with a cookie).

So要识趣 2024-08-13 04:26:27

有,但完全不直观。

curl_setopt($curl, CURLOPT_COOKIEFILE, "");

有关更多详细信息,请参阅我在评论中的回答

There is but it's completely unintuitive.

curl_setopt($curl, CURLOPT_COOKIEFILE, "");

For more details please see my answer in the comments

昨迟人 2024-08-13 04:26:27

如果使用 Linux,您可以将它们设置为指向 /dev/shm 中的某个位置。这会将它们保留在内存中,并且您可以放心,它们不会在重新启动后持续存在。

我以某种方式认为 Curl 的清理处理了 cookie 的取消链接,但我可能是错的。

If using Linux, you could set these to point somewhere within /dev/shm .. this will keep them in memory and you can be assured that they won't persist across re-boots.

I somehow thought that Curl's cleanup handled the unlinking of cookies, but I could be mistaken.

纸短情长 2024-08-13 04:26:27

对我有用的是使用此设置:

curl_setopt($ch, CURLOPT_HEADER, 1);

然后解析结果。我在这篇博文中找到了详细信息如何做到这一点。
由于它已经过时了,这里有一个 gist 替换已弃用的函数

What works for me is using this setting:

curl_setopt($ch, CURLOPT_HEADER, 1);

And then parsing the result. Details in this blog post where I found out how to do this.
And since that is old, here is a gist replacing deprecated functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文