I've got a script that I use in PHP to write tweets to a text file and use them on my website. I've also set up a cron job that will hourly run the script and fetch the latest tweets. This works and is set up correctly.
However, I'm noticing that sometimes it fails to get any tweets and writes to the file leaving it as 0KB and thus I get my clients ringing me saying it's broken.
Has anyone else experience this problem with the twitter API?
Here's the script:
<?php
session_start();
require_once("twitteroauth-master/twitteroauth/twitteroauth.php");
//Path to twitteroauth library
$twitteruser = "Username";
$notweets = 10;
$consumerkey = "****";
$consumersecret = "****";
$accesstoken = "*****";
$accesstokensecret = "*****";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
//Check twitter response for errors.
if ( isset( $tweets->errors[0]->code )) {
// If errors exist, print the first error for a simple notification.
echo "Error encountered: ".$tweets->errors[0]->message." Response code:" .$tweets->errors[0]->code;
} else {
// No errors exist. Write tweets to json/txt file.
$url = "/var/www/html/etc etc etc";
$file = $url.$twitteruser."-tweets.txt";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, json_encode($tweets));
fclose($fh);
if (file_exists($file)) {
echo $file . " successfully written (" .round(filesize($file)/1024)."KB)";
} else {
echo "Error encountered. File could not be written.";
}
}
?>