I have been working on creating a quick twitter widget for myself to re use, just something simple that caches the data from the public api. Since twitter only allows X requests for a certain time period and I have been testing this on a shared host, Its very often that I run out of requests and twitter denies my request. Thus I check first whether my request is denied, before writing an updated cached file.
Unfortunately I seem to lose this file from time to time, as I often see the 'temp file not written' message. Which should only appear if the file doesn't exist.
Here is the full php function:
function getTweets($num)
{
$cfile = sys_get_temp_dir().'/e1z'. $type . md5 ( 'something' );
if (is_file ( $cfile ) == false) {
$cfile_time = strtotime ( '1983-04-30 07:15:00' );
} else {
$cfile_time = filemtime ( $cfile );
}
$difference = strtotime ( date ( 'Y-m-d H:i:s' ) ) - $cfile_time;
if ($difference >= 100) {
$tags = array("created_at", "text", "screen_name", "profile_image_url"); // twitter names
$local = array("time", "msg", "user", "image"); // local names
$reader = new XMLReader();
$url = 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=boriskourt&include_rts=true&count=' . $num;
$headers = get_headers($url, 1);
if ($headers[0] == 'HTTP/1.0 400 Bad Request'){
if (is_file ( $cfile ) == true) {
$returner = file_get_contents ( $cfile );
touch ( $cfile );
file_put_contents ( $cfile, strval($returner) );
$returner = file_get_contents ( $cfile );
return $returner;
} else {
$returner = "<li><span>Temp file not written</span></li>";
return $returner;
}
} else {
$reader->open($url);
$i = 0;
$k = 1;
while ($i < $num)
{
$j = 0;
while ($reader->read() && $j < 4) // run through each tweet
{
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == $tags[$j])
{
while ($reader->read())
{
if ($reader->nodeType == XMLReader::TEXT && $j == 0)
{
if ($k) {
$tweets[$i][$local[$j]] = $reader->value;
$j++;
$k=0;}
else {$k=1;}
break;
}
else if ($reader->nodeType == XMLReader::TEXT)
{
$tweets[$i][$local[$j]] = $reader->value;
$j++;
break;
}
}
}
}
$i++;
}
$returner = "";
foreach ($tweets as $value) {
if ($value[user] != 'fugataquintet') {
$returner .= '<li class="retweet">';
} else {
$returner .= '<li>';
}
$messager = $value[msg];
$messager = " ".preg_replace( "/(([[:alnum:]]+:\/\/)|www\.)([^[:space:]]*)"."([[:alnum:]#?\/&=])/i", "<a href=\"\\1\\3\\4\" target=\"_blank\">"."\\1\\3\\4</a>", $messager);
$messager = preg_replace( "/ +@([a-z0-9_]*) ?/i", " <a href=\"http://twitter.com/#!/\\1\" target=\"_blank\">@\\1</a> ", $messager);
$messager = preg_replace( "/ +#([a-z0-9_]*) ?/i", " <a href=\"http://twitter.com/search?q=%23\\1\" target=\"_blank\">#\\1</a> ", $messager);
$returner .= '<span>'.$messager.'</span><a class="datereplace" href="http://twitter.com/#!/fugataquintet" title="'.$value[time].'">'.$value[time].'</a></li>';
}
touch ( $cfile );
file_put_contents ( $cfile, strval($returner) );
return $returner;
}
} else {
$returner = file_get_contents ( $cfile );
return $returner;
}
}
$cfileeven exist? You're checking if it exists, but has it been created?