- Home
- Categorie
- Coding e Sistemistica
- Coding
- Mettere condizione a simplexml se non si carica
-
Mettere condizione a simplexml se non si carica
Ciao a tutti, lo script per leggere un xml con simplexml è questo:
[PHP]$dato="http://www.xxxx/blog/feed/";$xml = simplexml_load_file($dato); foreach($xml->channel as $channel) { foreach($channel as $item){ echo '<div id="pette" style="float:left; width:830px; height:auto; margin-bottom:10px;"><strong>'.$item->title.'</strong>'.$item->description.'</div>'; } }
?> [/PHP]
A volte succede che non carica il feed e mi blocca tutto, come posso mettere una condizione perchè non accada questo ?
Ovvero : se non carica xml carica un messaggio...
-
Ciao pettedemon, potresti provare così:
[php]$dato="http://www.xxxx/blog/feed/";$xml = simplexml_load_file($dato);
if ($xml !== FALSE)
{
foreach($xml->channel as $channel)
{foreach($channel as $item){ echo '<div id="pette" style="float:left; width:830px; height:auto; margin-bottom:10px;"><strong>'.$item->title.'</strong>'.$item->description.'</div>'; } }
}
else
{
echo "Messaggio d'errore.";
}
?>[/php]:)
-
Grazie, peroò mi resta sempre il timeout di base di php... quindi prima di verificare la funzione aspetta 30 secondi
[[function.simplexml-load-file](http://www.radiocompany.com/function.simplexml-load-file)]: failed to open stream: Operation timed out in
-
Potresti provare con la funzione stream_set_timeout.