2012-09-13 186 views
5

我有這個腳本輸出一個RSS訂閱源。想要我想要做的就是嘗試訪問rss url,以獲得類似於5秒的上限,如果它不能,那麼我希望它加載服務器上的備份xml文檔。這是我有,它不工作:設置超時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 />'; 
    } 
} 

?> 

我得到一個超時錯誤,這就是全部。任何見解都會很棒!

回答

0

這是我的工作是什麼:

<?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

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

有一個更好的解決方案通過@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

更正上下文創建。不幸的是,我不再有任何超時,有沒有一個測試網站,只是一個很長的超時? – Daniel

+0

明白了,你可以通過調用一個包含'sleep(45);'命令的php來測試代碼。 – Daniel

+1

爲了考慮套接字超時,還有一個變量來設置所需的值:'ini_set('default_socket_timeout',5);'More information [here](http://stackoverflow.com/a/10236480) – jap1968