从 PHP 发送 HTTP 请求而不等待响应? [英] Send HTTP request from PHP without waiting for response?

查看:56
本文介绍了从 PHP 发送 HTTP 请求而不等待响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 PHP 发送一个 HTTP GET 请求.示例:

I want to have an HTTP GET request sent from PHP. Example:

http://tracker.example.com?product_number=5230&price=123.52

这个想法是做服务器端的网络分析:而不是发送跟踪从 JavaScript 到服务器的信息,服务器发送跟踪信息直接发送到另一台服务器.

The idea is to do server-side web-analytics: Instead of sending tracking information from JavaScript to a server, the server sends tracking information directly to another server.

要求:

  • 请求应该花费尽可能少的时间,以免明显延迟 PHP 页面的处理.

  • The request should take as little time as possible, in order to not noticeably delay processing of the PHP page.

来自 tracker.example.com 的响应不需要检查.例如,一些可能的回应来自tracker.example.com:

The response from the tracker.example.com does not need to be checked. As examples, some possible responses from tracker.example.com:

  • 200:很好,但不需要检查.

404:运气不好,但是 - 再次 - 无需检查.

404: Bad luck, but - again - no need to check that.

301: 虽然重定向是合适的,但它会延迟处理 PHP 页面,所以不要这样做.

301: Although a redirect would be appropriate, it would delay processing of the PHP page, so don't do that.

简而言之:所有响应都可以丢弃.

In short: All responses can be discarded.

解决方案的想法:

  • 在现已删除的答案中,有人建议调用命令行在 shell 进程中从 PHP curl.这似乎是个好主意,只是我不知道是否在下面分叉了很多 shell 进程重负荷是明智之举.

  • In a now deleted answer, someone suggested calling command line curl from PHP in a shell process. This seems like a good idea, only that I don't know if forking a lot of shell processes under heavy load is a wise thing to do.

我找到了 php-ga,一个用于做服务器端谷歌的包来自 PHP 的分析.在项目的页面上,它是提到:可以配置为 [...] 使用非阻塞请求."到现在还没抽时间去研究php-ga是什么方法内部使用,但这种方法可以!

I found php-ga, a package for doing server-side Google Analytics from PHP. On the project's page, it is mentioned: "Can be configured to [...] use non-blocking requests." So far I haven't found the time to investigate what method php-ga uses internally, but this method could be it!

简而言之:做通用服务器端的最佳解决方案是什么来自 PHP 的跟踪/分析.

In a nutshell: What is the best solution to do generic server-side tracking/analytics from PHP.

推荐答案

不幸的是,PHP 的定义是阻塞.虽然这适用于您通常要处理的大多数功能和操作,但当前的情况有所不同.

Unfortunately PHP by definition is blocking. While this holds true for the majority of functions and operations you will normally be handling, the current scenario is different.

我喜欢称之为 HTTP-Ping 的过程只需要您触摸 一个特定的 URI,强制特定的服务器引导它的内部逻辑.一些函数允许您通过不等待响应来实现与此 HTTP-ping 非常相似的事情.

The process which I like to call HTTP-Ping, requires that you only touch a specific URI, forcing the specific server to boot-strap it's internal logic. Some functions allow you to achieve something very similar to this HTTP-ping, by not waiting for a response.

注意ping一个url的过程,是一个两步的过程:

Take note that the process of pinging an url, is a two step process:

  1. 解析 DNS
  2. 提出请求

虽然在解析 DNS 并建立连接后发出请求应该相当快,但没有很多方法可以使 DNS 解析更快.

While making the request should be rather fast once the DNS is resolved and the connection is made, there aren't many ways of making the DNS resolve faster.

执行 http-ping 的一些方法是:

Some ways of doing an http-ping are:

  1. cURL,通过将 CONNECTION_TIMEOUT 设置为低值
  2. fsockopen 写入后立即关闭
  3. stream_socket_client(与 fsockopen 相同)并添加 STREAM_CLIENT_ASYNC_CONNECT
  1. cURL, by setting CONNECTION_TIMEOUT to a low value
  2. fsockopen by closing immediately after writing
  3. stream_socket_client (same as fsockopen) and also adding STREAM_CLIENT_ASYNC_CONNECT

虽然 cURLfsockopen 在解析 DNS 时都处于阻塞状态.我注意到 fsockopen 明显更快,即使在最坏的情况下也是如此.

While both cURL and fsockopen are both blocking while the DNS is being resolved. I have noticed that fsockopen is significantly faster, even in worst case scenarios.

stream_socket_client 另一方面应该解决有关 DNS 解析的问题,应该是这种情况下的最佳解决方案,但我还没有设法让它工作.

stream_socket_client on the other hand should fix the problem regarding DNS resolving and should be the optimal solution in this scenario, but I have not managed to get it to work.

一个最终的解决方案是启动另一个线程/进程来为您执行此操作.为此进行系统调用应该可以工作,但分叉当前进程也应该这样做.不幸的是,在您无法控制 PHP 运行环境的应用程序中,两者都不是真正安全的.

One final solution is to start another thread/process that does this for you. Making a system call for this should work, but also forking the current process should do that also. Unfortunately both are not really safe in applications where you can't control the environment on which PHP is running.

系统调用经常被阻止,并且默认情况下不启用 pcntl.

System calls are more often than not blocked and pcntl is not enabled by default.

这篇关于从 PHP 发送 HTTP 请求而不等待响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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