2011-10-12 3 views
0

는 클릭 이벤트에 RSS 뉴스 데이터를로드하기 위해 다음과 같은 AJAX 호출을 고려 :PHP와 AJAX : 캐시 RSS 데이터

include ('newsFeed.php'); 
    function printNewsMenu($itemD){ 
    for ($i = 0; $i < 4; $i++) { 
      if($itemD==null) 
      echo "An error has occured, please try again later"; 
      $article_title = $itemD[$i]->title; 
      $article_link = $itemD[$i]->link; 
      echo "<a href='".$article_link."' title='".$article_title."' target='_blank'>". $article_title. " </a></br>". PHP_EOL; 
} 
printNewsMenu($itemD); 

(가)와 같은 newsFeed.php 파일 lloks을 포함 :

$(function() { 
      $(document).ready(function() { 
       $('.msg_head').eq(0).click(function(){ 
        $('.msg_body').eq(0).load('printSideNews.php'); 
        $('.loadMessage').eq(2).hide(); 
       }); 
    }); 
}); 

printSideNews.php

는 다음과 같습니다 :

$urlD = "someurl"; 
    $xmlD = simplexml_load_file($urlD); 
    $itemD = $xmlD->channel->item; 

$itemD 또는을 캐시해야합니다.개체 - 어느 것이 확실하지 않습니다. 이 URL을 1 시간 동안 캐시 한 다음 URL에서 다시 가져와야합니다. 이 혼란을 숨기는 코드에 대한 도움은 크게 감사하겠습니다.

+0

무엇 당신의 ** 질문 ** 그리고 무엇을 당신이 당신의 문제를 해결하기 위해 지금까지 노력했다? 이것은 렌털 코더이지만 도움을주는 0은 아니므로, 시도한 내용과 실패한 부분을 명확히하지 않으면 잘못된 장소에 있습니다. –

+0

사용자에게 어떤 종류의 세션 상태가 있습니까? –

+0

사용자 기능을 구축하지 않겠습니다. 사이트에는 방문자 만 있습니다. 개념은 세션을 사용하지 않는 캐싱을위한 폴더를 갖는 것입니다. – George

답변

1

이 기능에 대해 살펴 :

<?php  
    function cacheObject($url,$name,$age = 86400) 
     { 
     // directory in which to store cached files 
     $cacheDir = "cache/"; 
     // cache filename constructed from MD5 hash of URL 
     $filename = $cacheDir.$name; 
     // default to fetch the file 
     $cache = true; 
     // but if the file exists, don't fetch if it is recent enough 
     if (file_exists($filename)) 
     { 
      $cache = (filemtime($filename) < (time()-$age)); 
     } 
     // fetch the file if required 
     if ($cache) 
     { 
      $xmlD = simplexml_load_file($url); 
      $itemD = $xmlD->channel->item; 
      file_put_contents($filename,serialize($itemD)); 
      // update timestamp to now 
      touch($filename); 
     } 
     // return the cache filename 
     return unserialize(file_get_contents($filename)); 
     }  


$urlD = "someurl"; 

$itemD = cacheObject($urlD,'cacheobject',3600); 
+0

현재 URL은 필요하지 않으므로 XAMPP 서버에서 localhost를 사용하고 있습니까? – George

+1

코드를 예제로 업데이트했습니다. ** newsFeed.php **는 피드의 1 시간 캐싱으로 보일 수 있습니다. –

+0

10 이제 도움이 되겠지만, 이제 구현할 것입니다. – George