使用PECL HTTP类在PHP中并行HTTP请求[答案:HttpRequestPool类] [英] Parallel HTTP requests in PHP using PECL HTTP classes [Answer: HttpRequestPool class]

查看:432
本文介绍了使用PECL HTTP类在PHP中并行HTTP请求[答案:HttpRequestPool类]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


HttpRequestPool 类提供了一个解决方案。非常感谢那些指出这一点的人。

The HttpRequestPool class provides a solution. Many thanks to those who pointed this out.

可以在以下网址找到一个简短的教程: http://www.phptutorial.info/?HttpRequestPool-construct

A brief tutorial can be found at: http://www.phptutorial.info/?HttpRequestPool-construct

问题

我想在PHP中进行并发/并行/同步HTTP请求。我想避免连续请求:

I'd like to make concurrent/parallel/simultaneous HTTP requests in PHP. I'd like to avoid consecutive requests as:


  • 一组请求需要很长时间才能完成;请求时间越长

  • 一个集合中途的一个请求的超时可能导致以后的请求不被发出(如果脚本有执行时间限制)

我已设法找到制作 simultaneousuos [sic] PHP中的cURL 请求,但是我想明确地使用PHP的 HTTP函数如果可能的话。

I have managed to find details for making simultaneuos [sic] HTTP requests in PHP with cURL, however I'd like to explicitly use PHP's HTTP functions if at all possible.

具体来说,我需要将数据同时发送到一个集合的URL。发布数据的网址超出了我的控制范围;它们是用户设置的。

Specifically, I need to POST data concurrently to a set of URLs. The URLs to which data are posted are beyond my control; they are user-set.

我不介意是否需要等待所有请求完成才能处理响应。如果我在每个请求上设置30秒的超时并同时发出请求,我知道我必须等待最多30秒(可能多一点)才能完成所有请求。

I don't mind if I need to wait for all requests to finish before the responses can be processed. If I set a timeout of 30 seconds on each request and requests are made concurrently, I know I must wait a maximum of 30 seconds (perhaps a little more) for all requests to complete.

我找不到如何实现这一目标的细节。但是,我最近注意到在PHP手册中提到PHP5 +能够处理并发HTTP请求 - 我打算在当时记下它,忘记了,再也找不到了。

I can find no details of how this might be achieved. However, I did recently notice a mention in the PHP manual of PHP5+ being able to handle concurrent HTTP requests - I intended to make a note of it at the time, forgot, and cannot find it again.

单个请求示例(正常工作)

<?php
$request_1 = new HttpRequest($url_1, HTTP_METH_POST);
$request_1->setRawPostData($dataSet_1);
$request_1->send();
?>

并发请求示例(不完整,清楚)

<?php
$request_1 = new HttpRequest($url_1, HTTP_METH_POST);
$request_1->setRawPostData($dataSet_1);

$request_2 = new HttpRequest($url_2, HTTP_METH_POST);
$request_2->setRawPostData($dataSet_2);

// ...

$request_N = new HttpRequest($url_N, HTTP_METH_POST);
$request_N->setRawPostData($dataSet_N);

// Do something to send() all requests at the same time
?>

任何想法都会非常感激!

Any thoughts would be most appreciated!

澄清1 :我想坚持PECL HTTP功能:

Clarification 1: I'd like to stick to the PECL HTTP functions as:


  • 他们提供了一个不错的选择OOP界面

  • 它们在相关应用程序中被广泛使用,并且从维护的角度来看,坚持使用已经使用的应该是有益的

  • 我一般与使用cURL相比,必须编写更少的代码行来使用PECL HTTP函数发出HTTP请求 - 从维护的角度来看,更少的代码行也应该是有益的

澄清2 :我意识到PHP的HTTP功能没有内置,也许我在那里做错了,我会更正。我不担心人们不得不安装额外的东西 - 这不是一个要分发的应用程序,它是一个带有服务器的Web应用程序。

Clarification 2: I realise PHP's HTTP functions aren't built in and perhaps I worded things wrongly there, which I shall correct. I have no concerns about people having to install extra stuff - this is not an application that is to be distributed, it's a web app with a server to itself.

澄清3 :如果有人权威地说PECL HTTP不能这样做,我会非常高兴。

Clarification 3: I'd be perfectly happy if someone authoritatively states that the PECL HTTP cannot do this.

推荐答案

我很确定 HttpRequestPool 正是您所需要的。

I'm pretty sure HttpRequestPool is what you're looking for.

稍微详细一点,您可以使用分叉来实现您正在寻找的东西,但这似乎不必要地复杂并且在HTML上下文中不是很有用。虽然我还没有测试过,但这段代码应该是这样的:

To elaborate a little, you can use forking to achieve what you're looking for, but that seems unnecessarily complex and not very useful in a HTML context. While I haven't tested, this code should be it:


// let $requests be an array of requests to send
$pool = new HttpRequestPool();
foreach ($requests as $request) {
  $pool->attach($request);
}
$pool->send();
foreach ($pool as $request) {
  // do stuff
}

这篇关于使用PECL HTTP类在PHP中并行HTTP请求[答案:HttpRequestPool类]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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