Parallel HTTP requests in PHP using PECL HTTP classes [Answer: HttpRequestPool class](PHP 中使用 PECL HTTP 类的并行 HTTP 请求【答案:HttpRequestPool 类】)
问题描述
<强>HttpRequestPool 类提供了一个解决方案.非常感谢指出这一点的人.
可以在以下位置找到简要教程:http://www.phptutorial.info/?HttpRequestPool-构造
问题
我想在 PHP 中发出并发/并行/同时的 HTTP 请求.我想避免连续请求:
- 一组请求需要很长时间才能完成;请求越多,时间越长
- 一个请求在一个集合中途超时可能会导致后面的请求无法发出(如果脚本有执行时间限制)
我设法找到了制作 simultaneuos [原文如此] 带有 cURL 的 PHP 中的 HTTP 请求,但是我想明确使用 PHP 的 如果可能,HTTP 函数.
具体来说,我需要将数据同时发布到一组 URL.发布数据的 URL 超出我的控制范围;它们是用户设置的.
我不介意在处理响应之前是否需要等待所有请求完成.如果我将每个请求的超时设置为 30 秒,并且请求是同时发出的,我知道我必须等待最多 30 秒(可能多一点)才能完成所有请求.
我找不到有关如何实现这一点的详细信息.不过最近我确实注意到PHP手册中提到了PHP5+能够处理并发HTTP请求——我当时打算记下来,忘记了,再也找不到了.
单个请求示例(工作正常)
setRawPostData($dataSet_1);$request_1->send();?>
并发请求示例(不完整,清晰)
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);//做一些事情同时发送()所有请求?>
任何想法将不胜感激!
澄清 1:我想坚持 PECL HTTP 功能:
- 他们提供了一个很好的 OOP 界面
- 它们在相关应用程序中被广泛使用,从维护的角度来看,坚持使用已经在使用的内容应该是有益的
- 与使用 cURL 相比,我通常需要编写更少的代码行来使用 PECL HTTP 函数发出 HTTP 请求 - 从维护的角度来看,更少的代码行也应该是有益的
澄清 2:我意识到 PHP 的 HTTP 函数不是内置的,也许我在那里措辞有误,我将更正.我不担心人们必须安装额外的东西 - 这不是一个要分发的应用程序,它是一个带有服务器的网络应用程序.
澄清 3:如果有人权威性地声明 PECL HTTP 无法做到这一点,我会非常高兴.
我很确定 HttpRequestPool 正是您要找的.p>
稍微详细说明,您可以使用分叉来实现您要查找的内容,但这似乎不必要地复杂并且在 HTML 上下文中不是很有用.虽然我还没有测试过,但这段代码应该是这样的:
<前>//让 $requests 是一个要发送的请求数组$pool = new HttpRequestPool();foreach ($requests as $request) {$pool->attach($request);}$pool->send();foreach ($pool as $request) {//做东西}The HttpRequestPool class provides a solution. Many thanks to those who pointed this out.
A brief tutorial can be found at: http://www.phptutorial.info/?HttpRequestPool-construct
Problem
I'd like to make concurrent/parallel/simultaneous HTTP requests in PHP. I'd like to avoid consecutive requests as:
- a set of requests will take too long to complete; the more requests the longer
- the timeout of one request midway through a set may cause later requests to not be made (if a script has an execution time limit)
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.
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.
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.
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.
Single request example (works fine)
<?php
$request_1 = new HttpRequest($url_1, HTTP_METH_POST);
$request_1->setRawPostData($dataSet_1);
$request_1->send();
?>
Concurrent request example (incomplete, clearly)
<?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!
Clarification 1: I'd like to stick to the PECL HTTP functions as:
- they offer a nice OOP interface
- they're used extensively in the application in question and sticking to what's already in use should be beneficial from a maintenance perspective
- I generally have to write fewer lines of code to make an HTTP request using the PECL HTTP functions compared to using cURL - fewer lines of code should also be beneficial from a maintenance perspective
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.
Clarification 3: I'd be perfectly happy if someone authoritatively states that the PECL HTTP cannot do this.
I'm pretty sure HttpRequestPool is what you're looking for.
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 }
这篇关于PHP 中使用 PECL HTTP 类的并行 HTTP 请求【答案:HttpRequestPool 类】的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP 中使用 PECL HTTP 类的并行 HTTP 请求【答案:HttpRequestPool 类】
基础教程推荐
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- HTTP 与 FTP 上传 2021-01-01