I have it set up in my PHP.ini file to timeout after 30 seconds. I want to keep this setting. I have a script that connects to an API and every once in a while (like 1 in 10 times), it can't connect so it spends the entire 30 seconds. This connection is via curl.
I have a logs table in my database and I would like before the timeout occurs to insert a new item. Is this possible via PHP to insert a row on timeout for only this particular script?
I have done my own research and can't find anything. Any help would be appreciated.
You can use register_shutdown_function to execute something on script shutdown. It will even be called on script timeout.
edit:
Alternatively you can use CURLOPT_TIMEOUT to set the max execution time for your curl execution to something below 30s and then look at the returned value to see if max execution was reached and then log to the database.
Related
How do I set the TimeOut for EXEC the Query or Stored Procedure in SqlSrv PHP
Because i am using PHP to call SQL-Server Stored Procedure.
For ex
"EXEC SP_Name"
some times its taking too long time, so the PHP page shows 500 Internal server error.
If possible to set the time limit the SP was stopped then i show the error description etc...
how do i fix this issue?
Thanks in Advance.
in your php.ini file set
max_execution_time = 60--or whatever time you wish
Refer This Link for more details
The #Jayasurya Satheesh's answer refers to the time of any php execution time, but not for db query exec time. Actually php lacks in a easy way to control the execution time of a db query.
It is more on the DB/SQL settings side. If you can define the TimeOut on the SQL Server side, this will return "timeout" to PHP.
Anyway, you can define set_time_limit(25) (where 25 is the number of seconds you want), You can put this in your php code before your SQL EXEC, not neccesary to deal with php.ini if you want to set_time_limit for specific php file. So, if the query takes more than 25 seconds, the php will stop execution.
Remember set_time_limit() does not affect other php scritps, it's only for the current file where it's placed
I need to one of my script (not all) works only five second. And if execution not finished yet (within five seconds) it should be dropped.
So I use
ini_set('max_execution_time', 5);
Also if I do
ini_get('max_execution_time');
it shows me five second, but script not interrupt after 5 seconds.
P.S
safe_mode = off
nginx -> php-fpm
set_time_limit(5) also has no effect
You can use set_time_limit function on top in your code as follows:
set_time_limit(5)
NB: If you dont place it on top of your code, supposed the php script has ran for 3 seconds and you called set_time_limit(5) so the total time of allowed execution would be 3+5 = 8 sec not 5sec as expected
Update
From php documentation:
Any time spent on activity that happens outside the execution of the
script such as system calls using system(), stream operations,
database queries, etc. is not included when determining the maximum
time that the script has been running. This is not true on Windows
where the measured time is real.
I've got a command-line PHP script that connects to SQL Server 2005 using an ODBC connection. It uses odbc_exec() to execute a stored procedure which may take a while to process, and I don't want it to timeout.
I've been unable to find anything in the PHP documentation regarding how to set the timeout for odbc_exec(). Does it default to an infinite wait?
There's only so much you can do here. PHP's default script execution time is set to 30 seconds. There are numerous ways to change this, including the set_time_limit() function (found in the manual), and extending execution time by using ini_set, or using odbc_setoption
//extend execution time
$num_minutes = 5;
ini_set('max_execution_time', (60*$num_minutes)); //set timeout to 5 minutes
You can also change this setting inside your php.ini file. However, all of these only control the scope within what your apache server can control. If the database itself has a timeout, it will still cut off your call and return an error, even if your personally set execution times are elevated.
According to php.net you can use odbc_setoption():
$result = odbc_prepare($conn, $sql);
odbc_setoption($result, 2, 0, 30);// 30 seconds timeout
odbc_execute($result);
So you can increase timeout to some bigger number depending on your need
i have this error:
Fatal error: Maximum execution time of 30 seconds exceeded in C:\AppServ\www\facebook\classes\burccek.class.php on line 56
(im using file_get_contents)
(in this program i post file_get_contents data to facebook user wall(offline_access))
It means the file_get_contents operation takes more time that the max execution time of PHP. If you need a longer time, add this line at the top of your file: set_time_limit($seconds);
However 30 seconds seems a long time already so there might be some other issue with your application.
If duration of posting file to FB is longer than 30s (default maximum execution time of php script), use
set_time_limit ( 120 );
(or more in seconds) before executing file_get_contents
When posting data to other URLs, you should rely on CURL or even in extreme case may go to socket level. Curl has better control on connection time outs to handle network latency, much more set of options. In some hosting environments or servers a sys admin may restrict what all php.ini settings you can change, though you can change set_time_limit
You can change your set_time_limit in your php.ini file to alter the maximum execution time that php can use for a script.
Is there an option to use with PostgreSQL Functions in PHP, so you can specify a maxim execution time for a query ? I don't want to enable this from the config file because only certain queries need to be restricted.
run from php a query before the main query
like
SET statement_timeout TO 5000;
You can use set_time_limit() According to PHP doc:
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.