Timeout not working in PHP Httpful request - php

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.

Related

fread is blocking if there is no data to read

here's my problem :
I'm trying to send some specific query data to a server and some servers( game servers ) don't respond to the query protocol and the script is just giving me a timeout on the fread($socket, 1024) function.
Fatal error: Maximum execution time of 10 seconds exceeded in E:\xampp\htdocs\file.php on line 85
Are you guys having an idea on how to just return false if there is no data to read ?
Sounds like you might want to use non-blocking sockets.
In PHP you can use the socket_set_nonblock function.

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.

Strange timeout behaviour using php soap client

I'm trying to make a proxy-like page that forwards an AJAX request to a SOAP server.
The browser sends 2 requests to the same page (i.e. server.php with different query string) every 10 seconds.
The server makes a soap call to the soap server depending on the query string.
All is working fine.
Then I put a sleep (40 secs) in the soap server to simulate a slow response and I also put a timeout on the caller to abort the call after some seconds.
server.php: Pseudo code:
$timeout = 10;
ini_set("default_socket_timeout", $timeout);
$id = $_GET['id'];
$wsdl= 'http://soapserver/wsdl'
$client = new SoapClient($wsdl,array('connection_timeout'=> $timeout));
print($client->getQuote($id));
If the browser sends an ajax request to http://myserver/server.php?id=IBM
the request stops after the timeout I set.
If I try to make a second call before the first stops, the second one doesn't not respect timeout.
i.e.
Request:
GET http://myserver/server.php?id=IBM
and after 1 second
GET http://myserver/server.php?id=AAP
Response:
after 10 seconds:
No data
after 20 seconds:
No data
I also tried to not use PHP SOAP and use curl instead but I got the same results.
I also tried to open 3 tabs on my browser and call:
http://myserver/server.php?id=IBM
http://myserver/server.php?id=AAP
http://myserver/server.php?id=MSX
The first one stops after 10 seconds, the second after 20 seconds and the third after 30 seconds.
Is this a normal behaviour or I miss something ?
Thanks in advance
You are probably starting sessions, and session_start() blocks a second call to it until the other request has 'freed' the session (in other words: has finished and will not write any data to the session anymore). For time consuming requests, don't start a session if you don't need one, and if you DO need one, get all the data that you need and then call session_write_close() BEFORE you doing the time-consuming thing. If you need to write to the session afterwards, just call session_start() again.

PHP Max Exection Timeout Notification

Is it possible to get Max Execution timeout notification so that i can use code clean up operations before the php script stops running?
Basically, i am trying to create a script that can do some changes/modifications to my database which has huge data pile. Also, this php script execution can be paused/resumed using a filelock.
You can either use set_error_handler() or register_shutdown_function().
Edit: See also: max execution time error handling
If you set 0 to set_time_limit, it will never stop :)
You can increase the limit time inside your code using:
set_time_limit(300); //time in seconds
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
or
changing the max_execution_time parameter inside your php.ini.
The default time is 30 seconds.

Call a function before the page is timeout in PHP

I have a PHP script that will run for a long time. I have set the execution time limit to 5 minutes by using set_time_limit() function. I would like to ask whether I can set the script to call a function when timeout limit is reached? like the page unload script in JavaScript or viewDidUnload function in NSViewController, in iPhone SDK ?
You can use register_shutdown_function() to register a callback, that is called, when the execution finishes. You should also have a look at connection handling.
You can do it by using register_shutdown_function() But this isn't good practice, remeber that you will catch also fatal error, parse errors etc.

Categories