PHP并发HTTP请求?

发布于 2024-12-08 11:45:24 字数 128 浏览 3 评论 0原文

我想知道在 PHP 中执行并发 HTTP 请求的最佳方法是什么?我有很多数据要获取,我宁愿一次执行多个请求来检索所有数据。

有人知道我该怎么做吗?最好以匿名/回调函数的方式...

谢谢,

汤姆。

I was wondering what the best way to do concurrent HTTP requests in PHP? I have a lot of data to get and i'd rather do multiple requests at once to retrieve it all.

Does anybody know how I can do this? Preferably in an anonymous/callback function mannor...

Thanks,

Tom.

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

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

发布评论

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

评论(4

别闹i 2024-12-15 11:45:24

您可以使用 curl_multi,它在单个curl下内部触发多个单独的请求处理。

但除此之外,PHP 本身不以任何方式/形状/形式“多线程”,并且不允许并行运行,除非通过严重的黑客攻击(多个并行脚本、一个脚本通过 exec() 启动多个后台任务等...... )。

You can use curl_multi, which internally fires off multiple separate requests under a single curl handle.

But otherwise PHP itself not in any way/shape/form "multithreaded" and will not allow things to run in parallel, except via gross hacks (multiple parallel scripts, one script firing up multiple background tasks via exec(), etc...).

心作怪 2024-12-15 11:45:24

您可以尝试 curl_multi() 或使用较低级别的函数 socket_select()

You can try either curl_multi() or use a lower level function socket_select()

用心笑 2024-12-15 11:45:24

您可以使用 HttpRequestPool http://www.php.net/manual/de/httprequestpool .construct.php

$multiRequests = array(
  new HttpRequest('http://www.google.com', HttpRequest::METH_GET),
  new HttpRequest('http://www.yahoo.com', HttpRequest::METH_GET)
  new HttpRequest('http://www.bing.com', HttpRequest::METH_GET)
);

$pool = new HttpRequestPool();
foreach ($multiRequests as $request)
{
  $pool->attach($request);
}

$pool->send();

foreach($pool as $request) 
{
  echo $request->getResponseBody();
}

you can use HttpRequestPool http://www.php.net/manual/de/httprequestpool.construct.php

$multiRequests = array(
  new HttpRequest('http://www.google.com', HttpRequest::METH_GET),
  new HttpRequest('http://www.yahoo.com', HttpRequest::METH_GET)
  new HttpRequest('http://www.bing.com', HttpRequest::METH_GET)
);

$pool = new HttpRequestPool();
foreach ($multiRequests as $request)
{
  $pool->attach($request);
}

$pool->send();

foreach($pool as $request) 
{
  echo $request->getResponseBody();
}
末蓝 2024-12-15 11:45:24

或者,如果您愿意,可以将数据作为 json 发送给您。在 php 中,您可以再次将其碎片整理为所有值。例如。

xhttp.open("GET", "gotoChatRoomorNot.php?q=[{"+str+"},{"+user1+"},{"+user2"}]", true);

在 php 中,您可以按照以下步骤取回数据: 如何我如何使用 PHP 从 JSON 中提取数据?

因此,以 json 格式创建一个字符串,然后通过 http 发送整个内容。
我认为你可以使用 xml 执行相同的行为,但我不知道 xml

Or if you want you can send you data as json. In php you can defragment it into all the values again. for eg.

xhttp.open("GET", "gotoChatRoomorNot.php?q=[{"+str+"},{"+user1+"},{"+user2"}]", true);

and in php you can follow this to get your data back: How do I extract data from JSON with PHP?

So make a string in json format and send the entire thing through http.
I think you can perform the same kind of behaviour with xml, but i am not aware of xml

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