2016-07-14 4 views
3

rssリンク(ロイター:トップニュース)で最新のITEMを取得したいと思います。新しいITEMが来ると、PHP/AJAXは古いITEMを新しいものPHPでRSSからXMLを解析し、AJAXを使用して渡す

私はPHPとAJAXでこれをやろうとしましたが、空白のページまたは「致命的なエラー:最大実行時間30秒がC:\ xampp \ htdocs \ test4.phpの11行目を超えています。

機能:

<?php function parserSide($feedURL) { $rss = simplexml_load_file($feedURL); $output = " 
<ul class='newsSide'>"; $i = 0; foreach ($rss->channel->item as $feedItem) { $i++; $output .= " 
    <li><a href='$feedItem->link' title='$feedItem->title'>" . $feedItem->title . "</a></li>"; if($i >= 1) break; } $output .= "</ul>"; return $output; } ?> 
<?php 
    if(isset($_GET['fetchOnly'])){ 
     $url = 'http://feeds.reuters.com/reuters/topNews'; 

     $response = array(); 
     $handle = fopen($url, 'r'); 
     if ($handle) { 
      while (($data = simplexml_load_file($url)) !== FALSE) 
      { 
       $response = parserSide($url);     
      } 
      fclose($handle); 
     } 
     echo $response; 
     die(); 
    } 
?> 
    <div id="responseText"></div> 
    <script> 
     // run the function, it will re-run itself 
     fetchRate(); 

     function fetchRate() { 
      // create the new AJAX Object 
      xmlhttp = new XMLHttpRequest(); 
      // this handles the request 
      xmlhttp.onreadystatechange = function() { 
       if (xmlhttp.readyState == XMLHttpRequest.DONE) { 
        // if the request came back successfully 
        if (xmlhttp.status == 200) { 
         // write the response to a div 
         div = document.getElementById("responseText") 
         div.innerHTML = xmlhttp.responseText; 
        } else { 
         // if the request had an error 
         div.innerHTML = div.innerHTML + '<br />Error fetching rates error code : ' + xmlhttp.status; 
        } 
        // rerun this function to fetch updates 
        setTimeout(fetchRate, 1000); 
       } 
      }; 
      // open the AJAX Object 
      xmlhttp.open("GET", "<?= basename(__FILE__) ?>?fetchOnly", true); 
      // send the AJAX request 
      xmlhttp.send(); 
     } 
    </script> 

事前に感謝

+0

これは、JavaScriptを使用して直接行うことができます。 – Iceman

+0

@icemanの後のdbにタイトルを追加するにはPHPが必要です – TheQuestionerMan

+0

okです。次にcをします。 – Iceman

答えて

1

は、単純なXMLは、ロードが失敗した場合はfalseを返すので、単にあなたのチェックのためにそれを使用します。一点信頼性チェック。

<?php 
    function parserSide($feedURL) { 
    $rss = simplexml_load_file($feedURL); 
    $output = ""; 
    if($rss){ 
     //simple_xml load was a success. It does everything 
     $output .= "<ul class='newsSide'>"; 
     $i = 0; 
     foreach ($rss->channel->item as $feedItem) { 
      $i++; 
      $output .= "<li><a href='$feedItem->link' title='$feedItem->title'>" . $feedItem->title . "</a></li>"; 
      if($i >= 1) break; 
     } 
     $output .= "</ul>"; 
    } 
    return $output; 
} 
if(isset($_GET['fetchOnly'])){ 
    $url = 'http://feeds.reuters.com/reuters/topNews'; 
    $handle = fopen($url, 'r'); 
    $response = parserSide($url); 
    echo $response; 
    die(); 
} 
?> 
<div id="responseText"></div> 
<script> 
    // run the function, it will re-run itself 
    fetchRate(); 
    function fetchRate() { 
     var div = document.getElementById("responseText") 
     div.innerHTML = "attempting to load data from server...."; 
     // create the new AJAX Object 
     xmlhttp = new XMLHttpRequest(); 
     // this handles the request 
     xmlhttp.onreadystatechange = function() { 
      if (xmlhttp.readyState == XMLHttpRequest.DONE) { 
       // if the request came back successfully 
       if (xmlhttp.status == 200) { 
        // write the response to a div 
        div.innerHTML = xmlhttp.responseText; 
       } else { 
        // if the request had an error 
        div.innerHTML = div.innerHTML + '<br />Error fetching rates error code : ' + xmlhttp.status; 
       } 
       // rerun this function to fetch updates 
       setTimeout(fetchRate, 1000); 
      } 
     }; 
     // open the AJAX Object 
     xmlhttp.open("GET", "?fetchOnly", true); 
     // send the AJAX request 
     xmlhttp.send(); 
    } 
</script> 
+1

パーフェクトありがとう@ice – TheQuestionerMan

+0

http://stackoverflow.com/questions/38682343/loop-of-rss-absorption-with-phpajaxどうすればパートを取得できますか? tnx @iceman – TheQuestionerMan

関連する問題