简体   繁体   English

如何添加缓存到Twitter api调用wordpress的php中的关注者计数?

[英]How to add cache to twitter api call for follower count in php for wordpress?

How do I add a cache function for this call to do display a twitter follower count? 如何为此调用添加缓存功能以显示Twitter的关注者计数? I'm creating a list of 50 twitter users and am showing their number of followers. 我正在创建50个Twitter用户的列表,并显示其关注者数量。 I would like for it to update every 24 hours. 我希望它每24小时更新一次。 I have a cache folder set up, just not sure how to do this efficiently. 我设置了一个缓存文件夹,只是不确定如何高效地执行此操作。

Here's the code I'm using to call the follower count. 这是我用来调用关注者人数的代码。

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

A simple workaround. 一个简单的解决方法。 Use a file to store it. 使用文件进行存储。 Let this be a script: myscript.php 让它成为一个脚本: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);
?> 

And in your site, read from the file and display. 在您的站点中,读取文件并显示。

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

Now put the myscript.php in a cron-job and execute it once every 24 hours. 现在将myscript.php放在cron作业中,每24小时执行一次。 And a .txt file isn't going to matter. .txt文件无关紧要。 No secrecy in any data in it. 其中的任何数据都不保密。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM