Example of How to Use Fastcgi_Finish_Request()

example of how to use fastcgi_finish_request()

I understand that basically I can call fastcgi_finish_request(); and then continue executing PHP script.

Yes, that's all you have to do.

$obj = new controller();
echo $o->getResult();
fastcgi_finish_request();
do_facebook_thing();

To convince yourself it is working, do this:

echo "Test";
fastcgi_finish_request();
sleep(10);

If you remove the second line, then you will see that the browser has to wait 10 seconds.

Can I use fastcgi_finish_request() like register_shutdown_function?

Yes, it's possible to use fastcgi_finish_request for that. You can save this file and see that it works:

<?php

$timeout = 3600; // cache time-out
$file = '/home/galymzhan/www/ps/' . md5($_SERVER['REQUEST_URI']); // unique id for this page
if (file_exists($file) && (filemtime($file) + $timeout) > time()) {
echo "Got this from cache<br>";
readfile($file);
exit();
} else {
ob_start();
echo "Content to be cached<br>";

$content = ob_get_flush();
fastcgi_finish_request();
// sleep(5);

file_put_contents($file, $content);
}

Even if you uncomment the line with sleep(5), you'll see that page still opens instantly because fastcgi_finish_request sends data back to browser and then proceeds with whatever code is written after it.

PHP-FPM fastcgi_finish_request() not working as expected

Next to the server-response itself (which you can control with the fastcgi_finish_request function and rest assured it works that way), there can be other resources that is blocking the (next) script from starting right ahead.

This can be file-lockings (popular for session) and other stuff. As you have not shared much code and we do not see your Kohana configuration you should take a look which components you use and which resources they acquire.

fastcgi_finish_request() undefined?

The reason I was receiving the "PHP Fatal Error" message was because I was calling the fastcgi_finish_request() method from a script that was not being executing through fastcgi. After researching this I now have a better understanding of php-fpm...so that's a plus :)

How do I know if fastcgi_finish_request() is available?

You can test with function_exists():

  • function_exists — Return TRUE if the given function has been defined

Checks the list of defined functions, both built-in (internal) and user-defined, for function_name […] Returns TRUE if function_name exists and is a function, FALSE otherwise.

Example usage:

if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
}

php close connection and continue on background

Call this: fastcgi_finish_request();

See also:

  • example of how to use fastcgi_finish_request()
  • http://www.php.net/manual/en/features.connection-handling.php (and comments)
  • http://www.php.net/manual/en/function.connection-aborted.php (and comments)


Related Topics



Leave a reply



Submit