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

查看:626
本文介绍了从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:


    <

301:虽然重定向是合适的,延迟
处理PHP页面,因此不要这样做。

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

解决方案的想法:


  • 在一个已删除的答案中,有人建议调用命令行
    curl 从PHP在shell进程。这似乎是一个好主意,
    只是我不知道是否在
    重载下分配很多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 ,这是一个用于执行服务器端Google $ b的包$ b来自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.

推荐答案

根据定义是阻止。虽然这对于你通常正在处理的大多数函数和操作都是成立的,但是当前的情况是不同的。

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.

请注意,


  1. 解析DNS

  2. 发出请求

一旦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

cURL c $ c> fsockopen 都在阻止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天全站免登陆