PHP: Send persistent asynchronous request -


I created this function a long time ago, which appeared right then

  Public static function sendAsyncHTTPRequest ($ hostName, $ port = 80, $ method, $ url, $ headers = array ()) {$ fp = fsockopen ($ hostName, $ port, $ error, $ errstr, 30); If (! $ Fp) {put new / exception ($ errstr, $ errno); } Other {Effilit ($ FP, "$ method $ Yuri HTTP / 1.1 \ r \ n" "Host:" $ hostName. "\ R \ n" "Connection: Closed \ r \ n" \ r \ n ", $ Header)." \ R \ n \ r \ n "); fclose ($ FP);}}  

The sole purpose of triggering some scripts from customer request, without slowing down the request, Without expecting a response, however, I tried to use that function today, to start a websocket server and surprisingly it was not asynchronous. Here is a piece of code that should start the server

  \ MF \ System :: sendAsyncHTTPRequest (SITE_DOMAI N, 80, 'GET', '/ battleWS / startServer /'. $ BattleId); Header ('Location:' .SITE_URL. '/ BattleWS / Field' '. Battle 1);  
< P> As you can see, I am starting the server and then redirecting the customer to the page that connects to the server. Apparently when the client is redirected to the server script that was executed That is unexpected for me because I believe that I will send an asynchronous request I was. I can confirm that because I'm had them put sleep in between the two lines I see the auto-shutdown countdown to the server log file. I tried switching from any fate to fsockopen to stream_socket_client In addition, this is the beginning of server-preliminary script (which is sendAsyncHTTPRequest () Has been called with)

  set_time_limit (0); Ignore_user_abort (true);  

Which is also confusing me, because ìgnore_user_abort has to execute the script.

I'm looking for a way to keep the server running without the use of libraries and frameworks, after redirecting the client to the original request.

You implement a command line PHP client in the background to the HTTP job. If it is executed in the background, then it is asynchronous

Example:

process.php This PHP script is run using PHP Cli.

execute.php this code is Apache (where you can see the current SendAsyncHTTPRequest method is executed.

  $ escaped_script = escapeshellarg (__dir__. '/process.php'); $ Escaped_args = escapeshellarg (json_encode (array ($ Hostname, $ port, $ method, $ yuri, $ header)) Exec ("/ usr / bin / php {$ escaped_script} {$ escaped_args}" / dev / null 2 ​​& gt; and 1 more Amp; echo -n \ $! ");  

Some details:

  & gt; / dev / null  

Standard output in the virtual file (like your echo, print, etc.) (All the outputs written in it are lost).

  2> and <1>  

error output will redirect to standard output , Write in the same virtual and non-existing file. This prevents you from being logged into apache2 / error.log. For example.

  & amp;  
< P> The most important thing in your case is this: Your execution of this $ order will be different:. Therefore the executive (immediatly) will continue to execute its php code and hopefully will create asynchronous behavior

  echo-n \ $!  

In response to your separate execution will be given to PID: it will be returned by exec () and you will be able to work with it (eg, this pid is a database Put it in and kill it after some time to avoid the zombies).


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -