2008/04/14 11:24

How to limit by Timeout when opening URL ?


Previously, I wrote the example to set timeout for fopen function. But it is not practical way to put timeout, because the configuration data is also modified by that function.

Here's another way to achieve the same function.

$query = "http://kurapa.com/api/?lic=" . $license_key . "&i=" . $ipaddress;
$url = parse_url($query);
$host = $url["host"];
$path = $url["path"] . "?" . $url["query"];
$timeout = 1;
$fp = fsockopen ($host, 80, $errno, $errstr, $timeout)
    or die('Can not open connection to server.');
if ($fp)
{
  fputs ($fp, "GET $path HTTP/1.0\nHost: " . $host . "\n\n");
  while (!feof($fp))
  {
    $buf .= fgets($fp, 128);
  }
  $lines = split("\n", $buf);
  $data = $lines[count($lines)-1];
  fclose($fp);
} else {
  # enter error handing code here
}
echo $data;


As you can see above, I used fsockopen function to put timeout.
1 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject Vicodin.

    Tracked from Vicodin detox. 2008/12/18 10:08 delete

    Vicodin withdrawls. Vicodin no prescription. Vicodin. Dangers of vicodin. Buy vicodin. Easing symptoms of vicodin withdrawal.