0

sorry for my English

I use file_get_contents function to get the HTML content of "www.example.com" as string and then process this string.

My problem is when this website take a lot of time to open, I want to set time out for a part of my script (not all script) so that if file_get_contents function take much time, my script will stop it and continue to next lines. I am using php5 is it possible to do this, I appreciate all your suggestions

example:

//set time out =  10S
$siteStr = @file_get_contents("www.example.com");
//10S is finished so stop file_get_contents and continue
Mohammad Alabed
  • 809
  • 6
  • 17

1 Answers1

1

use curl

 function get_data($url) {
    $ch = curl_init();
    $timeout = 10;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
Haim Evgi
  • 123,187
  • 45
  • 217
  • 223