使用 cURL 句柄作为数组键

发布于 2024-08-21 08:08:51 字数 651 浏览 5 评论 0原文

我使用curl_multi函数来请求多个URL并在它们完成时对其进行处理。当一个连接完成时,我真正拥有的只是来自 curl_multi_info_read() 的 cURL 句柄(以及相关数据)。

URL 来自作业队列,处理后我需要从队列中删除该作业。我不想依赖 URL 来识别作业(不应该有重复的 URL,但如果有怎么办)。

到目前为止我找到的解决方案是使用 cURL 句柄作为指向 jobid 的数组键。据我所知,当被视为字符串时,句柄类似于:

"Resource id #1"

这对我来说相当独特。基本代码是:

$ch = curl_init($job->getUrl());
$handles[$ch] = $job;
//then later
$done = curl_multi_info_read($master);
$handles[$done['handle']]->delete();
curl_multi_remove_handle($master, $done['handle']);

以这种方式使用 cURL 句柄安全吗?

或者是否有更好的方法将 cURL 句柄映射到创建它们的作业?

I'm using curl_multi functions to request multiple URLs and process them as they complete. As one connection completes all I really have is the cURL handle (and associated data) from curl_multi_info_read().

The URLs come from a job queue, and once processed I need to remove the job from the queue. I don't want to rely on the URL to identify the job (there shouldn't be duplicate URLs, but what if there is).

The solution I've worked up so far is to use the cURL handle as an array key pointing to the jobid. Form what I can tell, when treated as a string the handle is something like:

"Resource id #1"

That seams reasonably unique to me. The basic code is:

$ch = curl_init($job->getUrl());
$handles[$ch] = $job;
//then later
$done = curl_multi_info_read($master);
$handles[$done['handle']]->delete();
curl_multi_remove_handle($master, $done['handle']);

Is the cURL handle safe to use in this way?

Or is there a better way to map the cURL handles to the job that created them?

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

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

发布评论

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

评论(3

墨小沫ゞ 2024-08-28 08:08:51

将私有数据存储在 cURL Easy Handle 中,例如某些作业 ID:

curl_setopt($ch, CURLOPT_PRIVATE, $job->getId());
// then later
$id = curl_getinfo($done['handle'], CURLINFO_PRIVATE);

此“私有数据”功能尚未记录在 PHP 手册中。它已在 PHP 5.2.4 中引入。它允许您在 cURL 句柄内存储和检索您选择的字符串。将其用作唯一标识作业的键。

编辑:该功能现已在 PHP 手册中记录(搜索页面内的 CURLOPT_PRIVATE)。

Store private data inside the cURL easy handle, e.g. some job ID:

curl_setopt($ch, CURLOPT_PRIVATE, $job->getId());
// then later
$id = curl_getinfo($done['handle'], CURLINFO_PRIVATE);

This "private data" feature is not (yet) documented in the PHP manual. It was introduced already in PHP 5.2.4. It allows you to store and retrieve a string of your choice inside the cURL handle. Use it for a key that uniquely identifies the job.

Edit: Feature is now documented in the PHP manual (search for CURLOPT_PRIVATE within the page).

巷雨优美回忆 2024-08-28 08:08:51

由于某些隐式类型转换,它可能会起作用,但对我来说根本感觉不对。我认为这会在未来的某个地方带来麻烦,未来的版本会以不同的方式对待资源、不同的平台……

我个人不会这样做,而是使用数字索引。

It will probably work thanks to some implicit type cast, but it doesn't feel right to me at all. I think it's begging for trouble somewhere down the line, with future versions that treat resources differently, different platforms...

I personally wouldn't do it, but use numeric indexes.

玩套路吗 2024-08-28 08:08:51

我必须同意佩卡的观点……它可能会起作用,但闻起来很糟糕。 id 按照 Pekka 的建议使用直接整数,或者将句柄包装在一个简单的类中,然后使用 spl_object_hash 或让构造函数在设置时生成一个 uniqid。

I have to agree with Pekka... it will probably work but it smells bad. id use straight up integers as Pekka suggests or wrap the handles in a simple class and then use spl_object_hash or have the constructor generate a uniqid when its set up.

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