Home > Scipts > Twitter PHP scripts

Twitter PHP scripts

November 3rd, 2009 Spryt Leave a comment Go to comments

I’m a PHP programmer. No professional, but if I see the problem – I decide it. And Twitter – a new service for me, but some solution are not convenient for me. I learned the Twitter API to decide thise problems =)

API is very simple. Many operations can use simple HTTP request and XML report. Let’s get started:

About User
This operation shows all about user. Name, url, bio, followers, friends and twits count, creation date, last status and other. You can make user preview, followers counter, etc. Example:


<?php
function get_user($user,$is="user") {
if($is=="user"$content=file_get_contents("http://twitter.com/users/show.xml?screen_name=$user");
else if($is=="id"$content=file_get_contents("http://twitter.com/users/show.xml?user_id=$user"); 
preg_match_all("/<(.*)>(.*)<\/(.*)>/"$content$id); 
for($i=0$i<count($id[0]); $i++){ if($i==15) {$k="created";} else {$k=$id[1][$i];} $arr[$k]=$id[2][$i];} 
return $arr;}

//Example to counter:
$arr=get_user("User");
echo "Followers: $arr[followers_count]"//Example to preview:
$t=ceil((time()-strtotime($arr[created]))/86400);
if ($arr[followers_count]!=0) {$ratio=round($arr[friends_count]/$arr[followers_count],2);} else {$ratio="-";}
echo "<table border=0 width=900 style=\"border: 1px gray double;\">
<tr><td valign=top><img src=\"$arr[profile_image_url]\" width=48 height=48> 
<td valign=top><a href=\"http://twitter.com/$arr[screen_name]\">@$arr[screen_name]</a>: 
Ratio: $ratio - $arr[friends_count] | <b>$arr[followers_count]</b> | Twits: $arr[statuses_count] | $t days
<br>$arr[description] (<a href=\"$arr[url]\">$arr[url]</a>)<br>
<i>$arr[text]</i><br></table><br>";
?>

Followers and Friends
Get a follower and friends list of user (all who are not sealed off). Use to analysts.


<?php
function get_friends($user,$is="user") {
if($is=="user"$content=file_get_contents("http://twitter.com/friends/ids.xml?screen_name=$user");
else if($is=="id"$content=file_get_contents("http://twitter.com/friends/ids.xml?user_id=$user");
preg_match_all("/<id>(.*)<\/id>/"$content$ids); return $ids[1];}

function get_followers($user,$is="user") {
if($is=="user"$content=file_get_contents("http://twitter.com/followers/ids.xml?screen_name=$user");
else if($is=="id"$content=file_get_contents("http://twitter.com/followers/ids.xml?user_id=$user");
preg_match_all("/<id>(.*)<\/id>/"$content$ids); return $ids[1];}

//Example Friend of Follow
$name="User";
$fre=get_friends($name);
$fol=get_followers($name);
$friends=array_intersect($fre,$fol); echo count($friends)." Friends are following $name, and $name following them back<br>";
$following=array_diff($fre,$fol); echo count($following)." Following by $name, but they're not following $name back<br>";
$funs=array_diff($fol,$fre); echo count($funs)." Funs are following $name, but $name not following them back<br>";

foreach ($friends as $v) {$arr=get_user($v,"id"); echo "$arr[screen_name] |";} //Print names of friends
?>

Follow and UnFollow
It’s automatize follow. Need authorize, curl.


<?php
function friendship($username$password$twiname,$do){
if ($do=="follow") {$url "http://twitter.com/friendships/create/{$twiname}.xml";}
else if ($do=="unfollow") {$url ="http://twitter.com/friendships/destroy/{$twiname}.xml";}
$c curl_init();
curl_setopt($cCURLOPT_URL"$url");
curl_setopt($cCURLOPT_CONNECTTIMEOUT2);
curl_setopt($cCURLOPT_RETURNTRANSFER1);
curl_setopt($cCURLOPT_POST1);
curl_setopt($cCURLOPT_USERPWD"$username:$password");
$content curl_exec($c);
curl_close($c);
preg_match("/<following>(.*)<\/following>/"$content$ids);
return $ids[1];}

//Example:
friendship("User(you)","password","WhereFollow","follow");

//Follow all, who following you
$name="User"$pass="password";
$fre=get_friends($name); $fol=get_followers($name);
$funs=array_diff($fre,$fol);

foreach ($funs as $v) {echo "$v: ".friendship($name,$pass,$v,"follow").", "sleep(1);}
echo " followed (".count($funs).")";

//Unfollow All, who don't follow you:
$name="User"$pass="password";
$fre=get_friends($name); $fol=get_followers($name);
$following=array_diff($fre,$fol);

foreach ($following as $v) {echo "$v: ".friendship($name,$pass,$v,"unfollow").", "sleep(1);}
echo " unfollowed (".count($following).")";

//Follow all, who following Other user:
$user="FollowHisFriends";
$name="User"$pass="password";
$fre=get_friends_user($name)
$fre2=get_friends_user($user); 

$difre=array_diff($fre2,$fre); //Who follow User, but not following you

foreach ($difre as $v)  {echo "$v: ".friendship($name,$pass,$v,"follow").", "sleep(1);}
echo " followed (".count($difre).")";
?>

Limits
Request per hour – 150. Use many IP’s for more request.
Follow limit – 2000 OR 110% of your followers.

Usage
Use your own scripts to manage your Twitter Account – it’s free and full control. Use cron to automatic follow user, who following you. Create individual widgets, counter, previews and other – for great view on your blog. But.. don’t be evil – no spam, please.

Interesting? Subscribe on my RSS Feed and follow me on Twitter!

Categories: Scipts Tags:
  1. November 20th, 2009 at 21:14 | #1

    thanks for this script I was looking for it…..
    thanks a lot…

    [Reply]

  1. No trackbacks yet.

I'm not a robot