I am developing a web service where I need to call an Oracle procedure in PHP. The Oracle procedure will take time to process and after completion it will write in a table.
How do I return an error in the web service response if the procedure call is taking too much time?
Note: I am stuck with PHP 5.2 and I cannot install cURL.
Did you consider using http://php.net/manual/en/function.set-time-limit.php ?
Sets the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
You can set it for your specific script for less than 30s, so it stops after e.g. a few seconds. And then you just handle empty response / error response.
In php.ini default execution time limit is 30 seconds... Customise it & write your code in try catch & handle exception with customise error message.
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 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.
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.
I have a PHP script that fails when executing a long mysq_query. The error message is:
Fatal error: Maximum execution time of 400 seconds exceeded in....
I use XAMPP for windows and I have changed the php.ini file (there is only one in my installation), setting max_execution_time to a large value that is not 400 seconds. Nevertheless I keep getting the error message above....
Any idea of how to solve this?
Thanks
Beto
As Ignacio says, something may be overriding the php.ini setting. You can definitively set the timeout for that script using set_time_limit
set_time_limit(600);
You can also set it to 0, which removes any time restriction - although this is not best practice.
400 seconds is a huge amount of time for a query - have you looked into adding indexes on commonly used columns in your db?
You need to restart your web server for PHP to re-parse your config file.
Something in either a .htaccess file or within a PHP script is probably resetting it back to 400 seconds.
Having said that, 400 seconds is still an excessive amount of time for a query. You should consider farming the task off to another process if it really needs to take that long.