2

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.

2
  • I suspect you're struggling with the basic concept of the roundtrip request and response between the server-client. Commented Sep 21, 2011 at 0:03
  • In other words, a client requests a resource (index.php for example), in which case the server will do what the resource (in this case, a PHP script) requires, and return the result when it's finished to the requesting client. Commented Sep 21, 2011 at 0:05

5 Answers 5

3

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).

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

1

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.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.