AW: pulling content from a URL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



[..]
> into a variable.  I need to include some sort of error checking that 
> will kill this request if for some reason the URL request hangs for 
> more then 15 seconds.  In researching this, I think the correct 
> function to use is fsockopen, but I can't seem to get it to work.  Can 
> someone verify if fsockopen is the best way to grab an external URL, 
> but kill the request if it hangs for a certain amount of time ... and 
> if so, show me some sample code on how to place this URL's contents 
> into a variable that I can then parse.  The URL will be hard coded into 
[..]

stream_set_timeout should be what you're looking for.

might look like this:

<?php
$fp = fsockopen("www.example.com", 80);
if (!$fp) {
   echo "Unable to open\n";
} else {

   fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
   stream_set_timeout($fp, 2);
   $res = fread($fp, 2000);

   $info = stream_get_meta_data($fp);
   fclose($fp);

   if ($info['timed_out']) {
       echo 'Connection timed out!';
   } else {
       echo $res;
   }

}
?>
-- from the manual:
http://us4.php.net/manual/en/function.stream-set-timeout.php

$res will hold the first 2,000 bytes of the result. 

socket blocking would be another way, but the above one looks like exactly
what you're looking for

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux