Twitter Account Validation Script

This post was written by Brandon on September 2, 2009
Posted Under: PHP,Twitter

I have many Twitter accounts I need to manage, and recently a few of them have been hacked, or the passwords got changed for a few hours, then changed back. Instead of finding out too late when this happens, I decided to write a script that checks them, and emails me notifying me if the passwords are invalid.

I use my function from this post: Verifying Twitter Credentials using fsock

See the full article for more!

[ad#bigsquare]

 
<?php
$emailaddress = 'youremail@domain.com';
public function verifyCredentials($username, $password){
 
$out="GET http://twitter.com/account/verify_credentials.xml HTTP/1.1\r\n"
  ."Host: twitter.com\r\n"
  ."Authorization: Basic ".base64_encode ($username.":".$password)."\r\n"
  ."Content-type: application/x-www-form-urlencoded\r\n"
  ."Connection: Close\r\n\r\n";
 
$fp = fsockopen ('twitter.com', 80);
fwrite ($fp, $out);
$buf = fread($fp, 1024);
fclose ($fp);
 $pos = strpos($buf,'Could not');
  if ($pos === false){
  return 1;
}else{
  return 0;
    }
}
 
$u[0] = 'Twitteruser1';
$p[0] = 'twitterpass1';
$u[1] = 'Twitteruser2';
$p[1] = 'twitterpass2'';
$errors = 0;
$total = count($u);
for ($i = 0; $i<$total; $i++){
$TwitterValid = new TwitterValid($u[$i],$p[$i]);
$validation = verifyCredentials($u[$i],$p[$i]);
if ($validation == 0){
$v[$i] = 'INVALID';
$errors = 1;
}else{
$v[$i] = 'VALID';
}
}
if ($errors == 1){
for ($i = 0; $i<$total; $i++){
	$summary = $summary. "$u[$i] - $v[$i]\n";
}
$headers .= "MIME-Version: 1.0\n";
$headers = 'From: Validation@'.preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']))  . "\n";
$headers .= "Content-Type: text/plain\n";
$mail_Subject = "Twitter Accounts - Invalid!";
$mail_Body = "At least one account showed up with an invalid password!
Summary:
$summary
";
mail($emailaddress, $mail_Subject, $mail_Body,$headers);
}
 

So basically, you would add an aditional
$u[0] = 'Twitteruser1';
$p[0] = 'twitterpass1';
For each account, obviously incrementing the array size.

I use cronjobs each hour to run this script and it emails me only if any of the accounts appear invalid.

Add a Comment

required, use real name
required, will not be published
optional, your blog address