2012-08-17 4 views
0

나는 다양한 트위터 피드를 사용하여 짹짹을 잡았지만, 이제 나는 짹짹을 제한하고 캐싱하는 벽에 부딪쳤다. 나는 속도 제한을 칠 때까지문제 PHP로 짹짹 캐쉬하기

function tweets($twitter_handle, $tweet_limit, $tweet_links, $tweet_tags, $tweet_avatar, $tweet_profile) { 

    /* Store Tweets in a JSON object */ 
    $tweet_feed = json_decode(file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name='. 
        $twitter_handle.'&include_entities=true&include_rts=true&count='.$hard_max.'')); 

이 좋은 작품 : 여기에 내 코드입니다. 는 등 트위터의 아바타, 트윗 내용, 정확한 타임 스탬프를 반환해야 할 때

이 그러나 단지 사용자 이름과 (잘못) 타임 스탬프를 반환
function tweets($twitter_handle, $tweet_limit, $tweet_links, $tweet_tags, $tweet_avatar, $tweet_profile) { 

$url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name='.$twitter_handle.'&include_entities=true&include_rts=true&count='.$hard_max.''; 
$cache = dirname(__FILE__) . '/cache/twitter'; 

if(filemtime($cache) < (time() - 60)) 
{ 
    mkdir(dirname(__FILE__) . '/cache', 0777); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    $cachefile = fopen($cache, 'wb'); 
    fwrite($cachefile, $data); 
    fclose($cachefile); 
} 
else 
{ 
    $data = file_get_contents($cache); 
} 

$tweet_feed = json_decode($data); 

는, 또한, 그것은 또한입니다 : 여기 캐시 트윗에 추가거야 마다 몇 새로 고침 오류를 반환 :

경고 : MKDIR를() [function.mkdir] : 파일이에 /home/content/36/8614836/html/wp-content/themes/NCmainSite/functions.php에 존재 줄 110

도움이 될 것입니다. . 당신이 더 많은 정보가 필요하면
, 여기에 기능의 나머지 부분입니다 : http://snippi.com/s/9f066q0

+1

mkdir 오류는 충분히 분명합니다.이 기능을 실행할 때마다 캐시 디렉토리를 만들려고합니다. 디렉토리를 작성하려고 시도하기 전에 디렉토리가 존재하는지 점검해야합니다. –

+0

'mkdir()'앞에'@'를 추가하여 경고음을 없앱니다. 또는'mkdir()'을 호출하기 전에'is_dir()'을 사용하여 존재하는지 확인하십시오. – Florent

+1

오류 억제 기능을 사용하지 마십시오. 경고를 받으면 고칠 수 있습니다. –

답변

1
다음

이 필자가 당신의 문제를 해결하려고, 당신 플러스 컬의 불량 게시물 수신 거부를했다.

<?php 
function tweets($twitter_handle, $tweet_limit, $tweet_links, $tweet_tags, $tweet_avatar, $tweet_profile) { 

    $http_query = array('screen_name'=>$twitter_handle, 
         'include_entities'=>'true', 
         'include_rts'=>'true', 
         'count'=>(isset($hard_max))?$hard_max:'5'); 

    $url = 'http://api.twitter.com/1/statuses/user_timeline.json?'.http_build_query($http_query); 

    $cache_folder = dirname(__FILE__) . '/cache'; 
    $cache_file = $cache_folder . '/twitter.json'; 

    //Check folder exists 
    if(!file_exists($cache_folder)){mkdir($cache_folder, 0777);} 

    //Do if cache files not found or older then 60 seconds (tho 60 is not enough) 
    if(!file_exists($cache_file) || filemtime($cache_file) < (time() - 60)){ 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($ch, CURLOPT_REFERER, $_SERVER['REQUEST_URI']); 
     $data = curl_exec($ch); 
     curl_close($ch); 
     file_put_contents($cache_file,$data); 
    }else{ 
     $data = file_get_contents($cache_file); 
    } 

    return json_decode($data); 
} 

$twitter = tweets('RemotiaSoftware', 'tweet_limit','tweet_links', 'tweet_tags', 'tweet_avatar', 'tweet_profile'); 
print_r($twitter); 
?> 
+0

고마워요. 내가 포함 된 기능을 사용하기 위해 몇 가지 편집을 추가했습니다. –