How to handle mysql timeout issue in api php - php

I am working on a report section where user can access their set of data by year and dates
so there will be more then 5 to 6 queries in the api to retrieve the analytical data from the database. but the issue i am facing is it is getting timeout and returning a Error page of timeout in response instead i want to show custom error message to reduce the time interval of the dates Like "Please reduce the interval between dates to retrieve the data"
So how i can achieve this.Try catch i have used but didnt worked for me
try
{
$data1 =Database->Query1();
$data2 =Database->Query2();
$data3 =Database->Query3();
$data4 =Database->Query4();
$data5 =Database->Query5();
}catch(/Exception $exception){
#Show_custom_error
}
is there any other way by i can achieve this ?

You can set_time_limit in your script file or set it for the entire php installation in your php.ini file. Have a look at the link.
Also checkout this SO question/answers How to catch the fatal error: Maximum execution time of 30 seconds exceeded in PHP

Related

Timeout not working in PHP Httpful request

I am having an http request and I am using "Httpful Request" to send it in PHP.
I am setting a timeout of 20 seconds also in the request as follows:
$req = Request::get($Url);
$response = $req->timeoutIn(20)->expectsHtml()->send();
I was expecting to get an exception after timeout happens and I can handle the exception. But I am getting the following php Fatal error. Why is it so?
PHP Fatal error: Maximum execution time of 30 seconds exceeded in
phar://C:/CapPortal/cpPortal/source/wordpress/httpful.phar/Httpful/Request.php
on line 202
You can use set_time_limit($seconds) to set that limit higher, if you need more execution time. You can also set it to 0, which means infinite. Warning: Apache (if you're using php with it) may also limit php's execution time.
The httpful module, itself has some method to set time out name is timeoutIn().
So you can add this method to your code and set the time out, for example to 50 seconds:
$response = $req->timeoutIn(20)->expectsHtml()->timeoutIn(50)->send();
It work fine for me.

Timeout a PHP function after 5 seconds

I am making an API call to a service and need to timeout the call after 5 seconds and consider it a "fail" then proceed with the code. If it times out I want to save this to a $timeoutResult variable and then pass that all the way back to the javascript (I can do this part).
I'm just not sure how to do a timed function in PHP. I've seen the documentation on set_time_limit(5) but I'm not sure how to do it?
For example:
$response = $api_calls->apiCall($endpoint, $data); If this takes >5 seconds I want it to quit/consider the call a "fail" and then proceed onto my error handling further down the code.
I'm not sure how to stop the execution of THIS function by considering it a fail and proceeding.
Would something like this work?
set_time_limit(5);
$response = $api_calls->apiCall($endpoint, $data);
set_time_limit(0);
This way I set a timeout (which begins when this function inside a function is being called), it tries to execute, and if it finishes it then sets the time out back to infinity?
My cURL settings in apiCall() has a standard timeout of 10 seconds, but for this one particular call I need it to timeout after 5 seconds and then display an error if it times out.
You've not shown the code which actually makes the api call!
While its possible to to set a watchdog timer (SIGALRM) this is only an option on a POSIX system and only when running in the CLI sapi.
You mention that the code uses curl. This has lots of options for controlling timeouts - _CONNECTTIMEOUT[_MS], _LOW_SPED_LIMIT, _LOW_SPEED_TIME and _TIMEOUT[_MS] all documented in the manual.
I added an additional parameter to my apiCall() function which accepts an array.
I then looped through this array using
if(isset($extra_curl_options) && $extra_curl_options.length > 0){
foreach($extra_curl_options AS $k => $v) {
$http_request->setOption(constant($k), $v);
}
}
This will allow me to pass in multiple curl options to the apiCall for the future.

Timeout when trying to delete a record

The weirdest thing happened today. When I run this query:
DELETE FROM some_table WHERE id IN(5)
I get a 30 second timeout error in PHP. The same query runs without issues on my local development server, but when I move it to the production server, I get the timeout.
No sqlite error or anything like that, just "Fatal error: Maximum execution time of 30 seconds exceeded " :|
What could be the problem? Is there any way I could debug this at least?
In top of all my new codes I put this function
ini_set('max_execution_time',60);
reference .
to debug my script execute time I use this
$start = microtime(true);
function execute(){global $start;$end = microtime(true);$time=number_format(($end - $start), 5);return$time;}
//..... your code here
echo '<br><b>.'Page Loaded In 'execute().' Seconds<b/>';

Getting "Maximum execution time" error intermittently

I am facing this problem, firstly I would like to say that I am very new to PHP and MySQL.
Fatal error: Maximum execution time of 30 seconds exceeded in
.........................\cdn\wished.php on line 3
I don't know what is wrong in line 3, its giving error only sometimes. Here's my code:
<?php
//wished.php
$CheckQuery = mysql_query("SELECT * FROM users WHERE ownerid='$user->id'");
$wished = 0;
while($row = mysql_fetch_assoc($CheckQuery))
{
// echo $row['fname']."<br/>";
$wished++;
}
echo $wished;
?>
It was perfect when I run this in localhost with XAMPP. As soon as I hosted my app on my domain and used their database, it start getting error.
thanks every one :)
The issue is that the SQL query is taking too long, and your production PHP configuration has the PHP script time limit set too low.
At the beginning of the script you can add more time to the PHP time limit:
http://php.net/manual/en/function.set-time-limit.php
set_time_limit(60);
for example to add 30 more seconds (or use 0 to let the PHP script continue running).
If your production database is different than your development DB (and I'm assuming production has way more data) then it might be a really expensive call to get everything from the user table.

The bwshare module and PHP scraping

I wrote a script downloading a list of pages from a website. From time to time I receive the following error (the number of seconds is variable):
The bwshare module will refuse your requests for the next 7 seconds.
You have downloaded data too rapidly.
I found when using sleep(2) in the loop, it works much better, however the time delay is too expensive.
What's the best way how to deal with this module? Should I scrape it without any delay and if the response will be similar to the above message simply use sleep for the requested number of seconds?
It all depends on how many pages you can get before the error message.
Try and measure how many pages in average you can get.
4 pages before the bwshare message is the minimum.
If you are getting the error message before reaching 4 page downloads, then il would be faster to sleep(2) after each download.
try this way... it might help u.
$requestTime = 0.1; // s/connection
foreach(/* blah */) {
$start = microtime(true);
// Do your stuff to here.. get_file_content($url) and other processing .........
if($timeTaken = microtime(true)-$start < $requestTime) {
usleep(($requestTime-$timeTaken)*1000000);
}
}
if your problem is solved then try to post your answer so that other people may also be benefited

Categories