2012-06-06 21 views
0

如何爲此調用添加緩存功能以顯示Twitter關注者數量?我創建了一個包含50個Twitter用戶的列表,並顯示了他們的關注者人數。我希望它每24小時更新一次。我有一個緩存文件夾設置,只是不知道如何有效地做到這一點。如何添加緩存到twitter api調用php的wordpress跟隨者數量?

下面是我用來調用跟隨者計數的代碼。

<?php 
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/lookup.json?screen_name=eminem'), true); 
echo $data[0]['followers_count']; 
?> 

回答

1

一個簡單的解決方法。使用一個文件來存儲它。讓這是一個腳本:myscript.php

<?php 
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/lookup.json?screen_name=eminem'), true); 
$no_of_followers=$data[0]['followers_count']; 
$myfile=fopen('count.txt','w'); 
fwrite($myfile,$no_of_followers); 
fclose($myfile); 
?> 

並在您的網站,從文件中讀取和顯示。

$myfile=fopen('count.txt','r'); 
$no_of_followers=fgets($myfile); 
echo $no_of_followers; 
fclose($myfile); 

現在把myscript.php放在cron-job中,每24小時執行一次。一個.txt文件不會有問題。沒有任何數據保密。

相關問題