Jump to content

PHP Post to twitter - compare two files


techmonkey78

Recommended Posts

Hi,

 

I'm trying to write a script that posts the contents of a text file generated by radio automation software from a cron job every 30 minutes. The script should open this file (data.txt), compare it to a second file(title.txt), if it's different, post the new version to twitter and update title.txt. If it's not different, just die. The posting is working just fine, however, it will post duplicates as it's not doing the comparison part. Currently the station isn't 24 hours so it will just keep posting ad infinitum as it stands, if the station is off air, the two text files should be the same and not post.

 

Where am I going wrong here?

 

Many thanks in advance

Iain

<?php
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
#Consumer key token
$consumer_key = 'xxxxxxx';
#Consumer secret token
$consumer_secret = 'xxxxxxx';
#Access Token
$token = 'xxxxxx';
#Access Token Secret 
$secret= 'xxxxxxxx';
$twitterObj = new EpiTwitter($consumer_key, $consumer_secret, $token, $secret);
$twitterObjUnAuth = new EpiTwitter($consumer_key, $consumer_secret);
 $file = fopen("../data.txt","r");
 if(!file)
    {
      echo("ERROR:cant open file");
    }
    else
    {
      $buff = fread ($file,filesize("../data.txt"));
          } 
echo $buff;	

$refresh = "3600";  // Page refresh time in seconds. Put 0 for no refresh
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" >
<?php
if ($refresh != "0") 
	{
	print "<meta http-equiv=\"refresh\" content=\"$refresh\">\n";
	}
print "<title>TwiceCast - $buff</title>";
?>
</head>
<body><center>
<?php
$fh = fopen('title.txt', 'r'); 
$track = fread($fh, filesize('title.txt')); 
if ($track == $buff."\n"){ 
  fclose($fh); 
  die(0); 
}else{ 
  @fclose($fh); // if it errors, then the file doesn't exist, and the stream was never open 
  $fh = fopen('title.txt', 'w'); 
  fwrite($fh, $buff ."\n");
  fclose($fh);
  $twitterObj->post('/statuses/update.json', array('status' => "Recently played: " . $buff . "  #nowplaying http://bit.ly/kbridgeuk")); 
} 




?>
</center>


</body>
</html>




Link to comment
https://forums.phpfreaks.com/topic/280122-php-post-to-twitter-compare-two-files/
Share on other sites

One quick way to see if two files are the same is to compare the md5 hash of them. So if you md5() each file, them compare the hashes in a simple if statement, that will tell you if they are identical or not.

$file1 = file("path/to/file1");
$file2 = file("path/to/file2");
$file1Hash = md5($file1);
$file2Hash = md5($file2);
 
if($file1Hash == $file2Hash){
  //The files are the same
}else{
  //The files are different
}

I think that's what you'd be after..

 

Denno

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.