Re: Handle time-outs and errors with file()

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

 



On Jan 29, 2008 10:58 AM, John Papas <jspapas@xxxxxxxxx> wrote:

> I'm using file() to get the contents of a remote page in my script but
> I cannot find any information regarding how I could *gracefully*
> handle a broken network connection or even a time-out (slow
> connection).
>
> Is there a way?
>
> ---
> Example:
> $menu = file('http://www.remotesite.org/mypage.html'<http://www.remotesite.org/mypage.html%27>
> );
> foreach ($menu as $line_num => $line) {
>    echo $line."\n";
> }


theoretically, you could use a stream context and set a notification
handler, however, i was unable to get notifications when using this code:

function httpStreamNotificationHandler($notification_code, $severity,
$message, $message_code, $bytes_transferred, $bytes_max) {
    echo __FUNCTION__ . PHP_EOL;
    var_dump(func_get_args());
}
$options = array('http' =>
                    array('method' => 'GET',
                          'timeout' => 3
                    )
                );

$streamContext = stream_context_create($options);
if(stream_context_set_params($streamContext, array('notification',
'httpStreamNotificationHandler'))) {
    echo 'stream context parameters set, loading remote resource..' .
PHP_EOL;
    // load remote resource
    $theFile = file('http://www.remotesite.org/mypage.html', null,
$streamContext);
    var_dump($theFile);
} else {
    echo 'couldnt set context parameters' . PHP_EOL;
}

you might also get something out of the timeout option, which i set in the
code above,
but ive not verified its working correctly either.
also, you might try curl.

-nathan

[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