How do we stop Php function from executing if it cannot get results after X Seconds


We have a Php function that retrieves the content of a Site for Indexing into our Search engine.
This works fine, when the Site responds in normal time of a second or two.
But sometimes the Site does not respond in a timely fashion, so in this case, say after 5 Seconds we would like this Php function given up and return “No results”.

you can see the sample page containing this Php function here:

So if you go ahead and enter:

you will see that in a split second the function will return the selected Meta data of these Sites.
However, if you enter this Site for example:

you will see that the Php function cannot get this Site data, so it will just hang!.
So how do we tell the Php function to only wait for a reply from this Php function for only X Seconds and give up after that?

And here is the whole of these 2 Php functions that the above sample page POSTs to:


function file_get_contents_curl($url)
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);

    return $data;

function get_meta_data ($url) {

	$meta_data_list = array();
	$html = file_get_contents_curl("$url");

	//parsing begins here:
	$doc = new DOMDocument();
	$nodes = $doc->getElementsByTagName('title');

	ini_set('error_log', '/logs/specific_errors.php');
	error_reporting( -1 ); // maximum errors, warnings, etc
	ini_set( 'display_errors', false); //  Save in log BUT not Show on Screen

	$title = $nodes -> item(0) -> nodeValue;

	$metas = $doc->getElementsByTagName('meta');

	for ($i = 0; $i < $metas->length; $i++)
	    $meta = $metas->item($i);
		if($meta->getAttribute('name') == 'description')
			$description = $meta->getAttribute('content');
		if($meta->getAttribute('name') == 'keywords')
			$keywords = $meta->getAttribute('content');

	if (isset($title)) {
		array_push($meta_data_list, $title);
	} else {
		array_push($meta_data_list, null);

	if (isset($description)) {
		array_push($meta_data_list, $description);
	} else {
		array_push($meta_data_list, null);

	if (isset($keywords)) {
		$kw_list = explode(', ', $keywords);

		if (count($kw_list) > 0) {
			array_push($meta_data_list, $kw_list);
		} else {
			array_push($meta_data_list, null);

	return $meta_data_list;

}//Closes get_meta_data

$site_data = array();

if (isset($_POST['url'])) {

	$url = $_POST['url'];

	$result = get_meta_data ($url);

	if ($result) {

		$site_data = $result;

	} else {

		$site_data =  array_push($site_data, '<font color="red">Sorry but cannot access this Site</font>');



Have a look at the CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT parameters, described in the CURL documentation:

1 Like

droopsnoot, I am ware of this page on Php!
So what I was looking for, hoping for, is that someone could point me to the specific Code there to do what we want done.
Which is to cause a Php code from running after X seconds of no results.


Well, I’ve never used the CURL library myself, but I’d imagine that adding an extra line in the top of your file_get_contents_curl() function where you set all the other options, to add the timeout option, would be the way to go. Precisely which of the two timeout options you use depends on whether you want to trap connection timeouts, or overall timeouts.

So after much tweaking I got it working.
Man those Php pages about CURL TIMEOUT are not really clear at all :frowning:
But it is working after much tweaking.
Thanks :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.