PHP:http_get 如何工作?

发布于 2024-12-05 18:29:57 字数 140 浏览 0 评论 0原文

假设我有一个index.php,我在其中使用某种形式的http get/post 操作。服务器究竟是如何执行的?在完成执行之前它会暂停并等待响应吗?如果没有返回怎么办?如果我希望继续执行并在响应到达后执行另一个脚本(如 Ajax)怎么办?

启蒙赞赏。

Let's say I have an index.php where I use some form of http get/post operation. How exactly is this executed by the server? does it pause and wait for a response before completing execution? What if nothing is returned? What if I want the execution to continue and another script to be executed once the response arrives (as in Ajax)?

enlightenment appreciated.

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

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

发布评论

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

评论(5

悟红尘 2024-12-12 18:29:57

这是一个简单的逻辑问题。


在完成执行之前它是否会暂停并等待响应?

是的。


如果什么也没返回怎么办?

那么您要么得到false,要么得到一个空字符串。


如果我希望继续执行并在响应到达后执行另一个脚本(如在 Ajax 中),该怎么办?

你需要使用 libevent (不适合心软的人 -比 Ajax 难很多)。

It's a simple matter of logic.


Does it pause and wait for a response before completing execution?

Yes.


What if nothing is returned?

Then you either get false or a empty string.


What if I want the execution to continue and another script to be executed once the response arrives (as in Ajax)?

You need to play with libevent (not for the soft-hearted - a lot harder than Ajax).

没企图 2024-12-12 18:29:57

服务器收到请求(假设它是 Apache),它识别出有人正在请求 .php 文件,因此它知道必须将请求传递给 PHP 引擎。 PHP 引擎接收请求并将标头解析为 $_POST / $_GET / $_FILES ($_REQUEST) 超全局变量,以便可以使用。

在此期间执行如下:

客户端向服务器请求资源。
服务器接收它并执行某些工作以返回响应(在本例中它调用 PHP 引擎)。
PHP 引擎执行其必须执行的操作并返回结果(无论是有效结果还是解析错误 - 服务器并不关心)。无论如何,如果没有出现任何问题,服务器将返回带有适当响应状态代码的响应(2xx、3xx、4xx、5xx,您可能已经知道 404)。
一旦 Apache 收到 PHP 的响应,脚本执行就会停止。

它不是全双工通信,您可以随时打开套接字以用作电话线(想想 Skype 或任何其他 IM)。

对于 Javascript 和异步调用 - 由于 JS 是异步语言(它实现事件循环而不是线程模型),因此您可以指定响应到达时要执行的回调函数。根据您的需要,您可以向服务器发送另一个请求。

然而,WebSocket 协议支持全双工通信,使连接保持开放状态,服务器可以将数据推送到客户端。它需要与 Apache / Nginx 不同的服务器,例如 Node.js 或自定义服务器。

Server receives the request (let's say it's Apache), it recognizes someone is requesting a .php file so it knows it has to pass the request to PHP engine. PHP engine receives the request and parses the headers into $_POST / $_GET / $_FILES ($_REQUEST) superglobals so that it can be worked with.

During this time the execution is as follows:

Client requests a resource from the server.
Server receives it and does certain work to return response (in this case it invokes PHP engine).
PHP engine does what it has to do and returns a result (be it a valid result or a parse error - server doesn't care). In any way, if nothing went wrong server will return a response with appropriate response status code (2xx, 3xx, 4xx, 5xx, you probably know of 404 already).
Once Apache receives response from PHP, script execution is stopped.

It's not full-duplex communication where you can have socket open at all times to be used as a telephone wire (think Skype or any other IM).

In case of Javascript and async calls - since JS is asynchronous language (it implements an event loop rather than threaded model), you specify a callback function to be executed when the response arrives. Depending on what you need, you can send yet another request to the server.

However, there's the WebSocket protocol that enables full-duplex communication which leaves the connection open and where server can push the data to the client. It requires a different server than Apache / Nginx such as Node.js or a custom one.

忘你却要生生世世 2024-12-12 18:29:57

从文档中可以看出,http_get 似乎是一个阻塞调用,即它将冻结您的脚本,直到 HTTP 事务完成、失败或超时。看来你不能将其设置为非阻塞模式,并且PHP没有线程。我不是 PHP 专家,但我认为没有简单的方法来继续该脚本。

除了问题本身之外,如果我是你,我真的会重新考虑我的选择。我觉得你的想法不正确,因为我很难想象在 PHP 中严格需要执行 HTTP GET 的场景。这样做的情况非常非常少。

Reading from the docs, it seems like http_get is a blocking call, i.e. it will freeze your script until the HTTP transaction completes, fails or timeouts. It seems like you cannot set it in non-blocking mode, and PHP has no threads. I'm not an expert in PHP, but I think there's no easy way to continue the script.

Besides the question itself, if I were you, I would really reconsider my choices. I feel like you're not thinking it the right way, because I can hardly imagine a scenario where it's strictly needed to perform an HTTP GET in PHP. It is done very, very rarely.

开始看清了 2024-12-12 18:29:57

PHP 脚本不会以任何方式继续运行,除非页面仍在传递到浏览器。如果浏览器的“正在加载”图标没有旋转,则 PHP 已停止执行。它们运行然后几乎立即终止(对于合理大小的页面)。

当您传递 HTTP GET/POST 信号时,您将其传递给 PHP 脚本,但不是已经在运行并等待响应的脚本。这是脚本的一个全新实例,它必须重新分配所有内容,重新包含所有内容,并从数据库中重新获取所有内容(如果您使用的是数据库)。

PHP scripts do not continue running in any fashion unless the page is still being passed to the browser. If your browser's "Loading" icon isn't spinning, then PHP has stopped being executed. They run and then terminate almost instantaneously (for reasonably-sized pages).

When you pass an HTTP GET/POST signal, you're passing it to a PHP script, but not one that is already running and waiting for a response. It's an entirely new instantiation of the script, which has to re-assign everything, re-include everything, and re-grab everything from the database, if you're using one.

野侃 2024-12-12 18:29:57

index.php 将立即执行并终止。

如果有一个请求发布到 php,则 php 文件将被执行(同样,如果它是 index.php)并终止。
您可以使用 exec() 函数来执行 php 文件中的脚本。

The index.php will be executed and terminated right away.

If there's a request post to the php, the php file will be executed (again, if it's index.php) and terminated.
You can use exec() function to execute your script in your php file.

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