2012-09-13 24 views
5

Tengo este script que genera un feed rss. Lo que quiero hacer es intentar que llegue a la URL de rss para algo así como 5 segundos como máximo, y si no puede, entonces quiero que cargue un documento de copia de seguridad xml que esté en el servidor. Esto es lo que tengo y no está funcionando:Establecer el tiempo de espera en simplexml_load_file

<?php 

include '../php/connect.php'; 
$metaData = mysql_query("SELECT * FROM `siteinfo`") or die("couln't find table :("); 
$displayData = mysql_fetch_assoc($metaData); 
$url = $displayData['status']; 
$xml = file_get_contents($url); 

stream_set_timeout($xml, 5); 

if ($xml == FALSE) { 

    $xml = simplexml_load_file('backUpXml.xml'); 

    foreach ($xml->channel->item as $item) { 
     echo '<a href="'.$item->guid.'" alt="'.$item->title.'" target="_blank">', substr($item->title, 0, 62), '...</a><br /><span>', substr($item->pubDate, 4, 18),'</span><br /><hr /><br />'; 
    } 
} else { 

    $xml = simplexml_load_file($url); 

    foreach ($xml->channel->item as $item) { 
     echo '<a href="'.$item->guid.'" alt="'.$item->title.'" target="_blank">', substr($item->title, 0, 62), '...</a><br /><span>', substr($item->pubDate, 4, 18),'</span><br /><hr /><br />'; 
    } 
} 

?> 

Recibo un error de tiempo de espera y eso es todo. ¡Cualquier idea sería genial!

Respuesta

0

Esto es lo que tengo trabajo:

<?php 

    include 'php/connect.php' ; 
    $metaData = mysql_query("SELECT * FROM `siteinfo`") or die("couln't find table :("); 
    $displayData = mysql_fetch_assoc($metaData); 
    $url = $displayData['status']; 
    $xml = file_get_contents($url); 

    if (!$xml) { 

     $xml = simplexml_load_file('content/backUpXml.xml'); 

     foreach ($xml->channel->item as $item) { 
      echo '<a href="'.$item->guid.'" alt="'.$item->title.'" target="_blank">', substr($item->title, 0, 62), '...</a><br /><span>', substr($item->pubDate, 4, 18),'</span><br /><hr /><br />'; 
     } 
    } else { 

     $myFile = "content/backUpXml.xml"; 
     $fh = fopen($myFile, 'w') or die("can't open file"); 
     $stringData = $xml; 
     fwrite($fh, $stringData); 
     fclose($fh); 

     $xml = simplexml_load_file($url); 



     foreach ($xml->channel->item as $item) { 
      echo '<a href="'.$item->guid.'" alt="'.$item->title.'" target="_blank">', substr($item->title, 0, 62), '...</a><br /><span>', substr($item->pubDate, 4, 18),'</span><br /><hr /><br />'; 
     } 
    } 

    ?> 
0

De php.net:

<?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); //I think this is what you need. 
    fclose($fp); 

    if ($info['timed_out']) { //So you can check this variable. 
     echo 'Connection timed out!'; 
    } else { 
     echo $res; 
    } 

} 
?> 
1

hay una solución mucho mejor señalado por @AnthonySterling here:

function simplexml_load_file_from_url($url, $timeout = 5){ 
    $opts = array('http' => array('timeout' => (int)$timeout)); 
    $context = stream_context_create($opts); 
    $data = file_get_contents($url, false, $context); 
    if(!$data){ 
    trigger_error('Cannot load data from url: ' . $url, E_USER_NOTICE); 
    return false; 
    } 
    return simplexml_load_string($data); 
} 
+0

Corregida creación del contexto . Lamentablemente, ya no tengo ningún tiempo de espera, ¿hay un sitio de prueba que simplemente hace un tiempo de espera largo? – Daniel

+0

Entendido, puedes probar el código llamando a un php que contiene un comando 'sleep (45);'. – Daniel

+1

Para tener en cuenta también el tiempo de espera del socket, hay una variable para establecer el valor deseado: 'ini_set ('default_socket_timeout', 5);' Más información [aquí] (http://stackoverflow.com/a/10236480) – jap1968

Cuestiones relacionadas