Forum Moderators: coopster

Message Too Old, No Replies

Simple HTML DOM: how to set timeout

         

flapane

4:23 pm on Jun 18, 2012 (gmt 0)

10+ Year Member



I need to retrieve some html code and then parse a couple of divs
$html = file_get_html('http://foo.com/bar/');


What happens when foo.com is down? And what about the default time to wait before declaring a timeout? How to set a default message in case of timeout?

Thanks

coopster

11:41 pm on Jun 18, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



file_get_html is not a PHP function so you will have to look at the user-defined function for the "how-to" handlers.

flapane

7:35 am on Jun 19, 2012 (gmt 0)

10+ Year Member



Hi,
that's why I added "Simple HTML DOM" in the title. However, it doesn't have a documentation as detailed as PHP's, and its support forum on Sourceforge is empty, so I had to rely on this forum.

rocknbil

4:28 pm on Jun 19, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HTML DOM indicates a Javascript timeout, is this what you're asking for? Or a PHP timeout?

Timeouts for "browser cannot reach a web site" are specific to the browser and can be adjusted, there is no standard timeout setting. It really depends on how long you think your user's attention span is. :-)

Graceful degradation is usually easy. As mentioned, this should be handled in whatever function file_get_html() is, but with what you've given so far, in a PHP context,

$html = file_get_html('http://foo.com/bar/');

if (empty($html)) {
echo "<p>Please try later, we couldn't connect with that web site.</p>";
}

flapane

5:01 pm on Jun 19, 2012 (gmt 0)

10+ Year Member



The problem is that I don't know how to set the number of seconds after which timeout is declared (and $html is empty). Maybe I wasn't lucky enough while searching into Simple HTML DOM documentation.

Such code will be included in a PHP page, and an eventual timeout on an <? include ?> will heavily affect the main page.
So I decided to discard <? include ?> and choose an AJAX function instead.
If you consider that:
- the main page already needs jquery, so I'm not actually adding kylobytes to the page
- the code parsed from $html will ALREADY needs Javascript, so users without JS aren't really affected by jquery
- such output doesn't need to be indexed by search engines
- in case of timeout (and only when the cachefile has expired) the main page won't be affected, and a nice "Loading..." text will appear in that div
- today I realized that the output should be refreshed every 100 seconds

I thought that it could have been a good idea to use this function (we've gone OT in this way, but in case somebody has the same problem I thought that it was worth to post my solution):
(function($)
{
$(document).ready(function()
{
$.ajaxSetup(
{
cache: false,
beforeSend: function() {
$('#foo_content').hide();
$('#foo_loading').show();
},
complete: function() {
$('#foo_loading').hide();
$('#foo_content').show();
},
success: function() {
$('#foo_loading').hide();
$('#foo_content').show();
}
});
var $container = $("#foo_content");
$container.load('foo.php');
var refreshId = setInterval(function()
{
$container.load('foo.php');
}, 100000); //refresh every 100 seconds
});
})(jQuery);