PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > Handle time-outs and errors with file()
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Handle time-outs and errors with file()

Réponse
 
LinkBack Outils de la discussion
Vieux 29/01/2008, 15h58   #1
John Papas
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Handle time-outs and errors with file()

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');
foreach ($menu as $line_num => $line) {
echo $line."\n";
}
  Réponse avec citation
Vieux 29/01/2008, 17h47   #2
Nathan Nobbe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Handle time-outs and errors with file()

On Jan 29, 2008 10:58 AM, John Papas <jspapas@gmail.com> 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

  Réponse avec citation
Vieux 29/01/2008, 18h11   #3
Eric Butera
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Handle time-outs and errors with file()

On Jan 29, 2008 10:58 AM, John Papas <jspapas@gmail.com> 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');
> foreach ($menu as $line_num => $line) {
> echo $line."\n";
> }
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


I'm not exactly sure what you are trying to accomplish or what sort of
access you have to the server, but if it were me I wouldn't rely on a
remote connection in real time. I would have a cron job that
periodically updated the results that I am expecting so that reads on
your site are very fast. Like I said though I'm not sure what you're
using this for so I cannot say whether or not it is okay for you to
cache this data locally for 5 minutes or even 1 day.

If you can cache it but cannot run cron jobs, then just write the
return buffer to a file and hold that for as long as possible checking
the last time it was wrote to see if it needs a refresh.
  Réponse avec citation
Vieux 31/01/2008, 01h08   #4
Richard Lynch
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] Handle time-outs and errors with file()



On Tue, January 29, 2008 9:58 am, John Papas 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');


Use http://php.net/set_error_handler

You can change the timeout with set_ini() right before the file() call.

If $menu === false, then it failed.

> foreach ($menu as $line_num => $line) {
> echo $line."\n";
> }
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 23h14.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13834 seconds with 12 queries