user1438786
user1438786

Reputation: 1

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? I'm creating a list of 50 twitter users and am showing their number of followers. I would like for it to update every 24 hours. 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'];
?>

Upvotes: 0

Views: 585

Answers (1)

Arjun Abhynav
Arjun Abhynav

Reputation: 543

A simple workaround. Use a file to store it. Let this be a script: 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. And a .txt file isn't going to matter. No secrecy in any data in it.

Upvotes: 1

Related Questions