note 53564 added to function.set-time-limit
| From: | ericpecoraroatshepardcom at osu1 dot php dot net | Date: | Mon, 06 Jun 2005 07:57:57 +0000 |
| Subject: | note 53564 added to function.set-time-limit | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89994@lists.php.net to get a copy of this message | ||
I was having trouble with script timeouts in applications where the user prompted long running
background actions. I wrote this cURL/CLI background script that solved the problem when making
requests from HTTP.
<?php
/* BACKGROUND CLI 1.0
eric pecoraro _at_ shepard dot com - 2005-06-02
Use at your own risk. No warranties expressed or implied.
Include this file at the top of any script to run it in the background
with no time limitations ... e.g., include('background_cli.php');
The script that calls this file should not return output to the browser.
*/
# REQUIREMENTS - cURL and CLI
if ( !function_exists('curl_setopt') OR !function_exists('curl_setopt') ) {
echo 'Requires cURL and CLI installations.' ; exit ;
}
# BUILD PATHS
$script = array_pop(explode('/',$SCRIPT_NAME)) ;
$script_dir = substr($SCRIPT_NAME,0,strlen($SCRIPT_NAME)-strlen($script)) ;
$scriptURL = 'http://'. $HTTP_HOST . $script_dir .
"$script" ;
$curlURL = 'http://'. $HTTP_HOST . $script_dir .
"$script?runscript=curl" ;
# Indicate that script is being called by CLI
if ( php_sapi_name() == 'cli' ) {
$CLI = true ;
}
# Action if script is being called by cURL_prompt()
if ( $runscript == 'curl' ) {
$cmd = "/usr/local/bin/php ".$PATH_TRANSLATED ; // server location of script to run
exec($cmd) ;
exit;
}
# USER INTERFACE
// User answer after submission.
if ( $post ) {
cURL_prompt($curlURL) ;
echo '<div style="margin:25px;"><title>Background
CLI</title>';
echo 'O.K. If all goes well, <b>'.$script.'</b> is working hard in
the background with no ' ;
echo 'timeout limitations. <br><br><form action='.$scriptURL.'
method=GET>' ;
echo '<input type=submit value=" RESET BACKGROUND CLI
"></form></div>' ;
exit ;
}
// Start screen.
if ( !$CLI AND !$runscript ) {
echo '<title>Background CLI</title><div
style="margin:25px;">' ;
echo '<form action='.$scriptURL.' method=POST>' ;
echo 'Click to run <b>'.$script.'</b> from the PHP CLI command
line, in the background.<br><br>' ;
echo '<input type=hidden value=1 name=post>' ;
echo '<input type=submit value=" RUN IN BACKGROUND
"></form></div>' ;
exit ;
}
# cURL URL PROMPT FUNCTION
function cURL_prompt($url_path) {
ob_start(); // start output buffer
$c=curl_init($url_path);
curl_setopt($c, CURLOPT_TIMEOUT, 2); // drop connection after 2 seconds
curl_exec($c);
curl_close($c);
ob_end_clean(); // discard output buffer
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.set-time-limit.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53564
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53564&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53564&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53564&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53564&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53564&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53564&report=yes
Search -- http://master.php.net/manage/user-notes.php