I have some PHP code that retrieves Twitter JSON data from its API. I'm grabbing a few things like the userpic, screen name, no. of followers & no. of tweets.
And probably even more data later on for other profiles!
Here's the PHP code:
<?php
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/show.json?callback=?&screen_name=guardian'), true);
$data[0]['profile_image_url'];
?>
<?php
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/show.json?callback=?&screen_name=guardian'), true);
echo $data[0]['screen_name'];
?>
<?php
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/show.json?callback=?&screen_name=guardian'), true);
echo $data[0]['followers_count'];
?>
<?php
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/show.json?callback=?&screen_name=guardian'), true);
echo $data[0]['statuses_count'];
?>
It was all working well, and then it wasn't anymore. I'm assuming this is due to a rate limit/the fact that I have no caching for it.
I'm new to all this so how would I setup the caching for it and have it request the latest data every few hours or so / basically ensure that it doesn't stuff up & show nothing >.<
Really appreciate any help!!
It could be because of rate limiting.
Twitter enforces rate limiting on unauthenticated calls (calls made to the API that haven't been authenticated using OAuth).
"Unauthenticated calls are permitted 150 requests per hour. Unauthenticated calls are measured against the public facing IP of the server or device making the request."
If you are using shared hosting, it makes it more likely for you to get rate-limited as someone else using the same IP on the host could also be querying the Twitter API (hence, counting towards the hourly limit for that IP).
You can read more on these restrictions on Twitter's Rate Limiting restrictions website as well as on the Rate Limiting FAQ website.
Not $data[0]['profile_image_url']
Just put $data['profile_image_url']