twitter-2-identica

With this script, a 2nd identi.ca account (its not possible to send a direct message to yourself, so you need a 2nd account) and a cronjob you can subscribe to a twitter account without being registered to twitter. You will receive the tweets as a personal message in identi.ca account.

Installation

1. Create the following table (just run this sql query in phpmyadmin):
CREATE TABLE IF NOT EXISTS twitter2identica (
  twitter_primary mediumint(9) NOT NULL auto_increment,
  twitter_name text NOT NULL,
  twitter_id bigint(20) NOT NULL,
  twitter_to text NOT NULL,
  PRIMARY KEY  (twitter_primary)
)


2. Insert data to the table:
twitter_primary=leave this empty
twitter_name=name of the twitter-account (e.g. THE_REAL_SHAQ)
twitter_id=0 (the id of the latest tweet you received; leave it 0 at first!)
twitter_to=your identi.ca account (e.g. miga)


3. Create a 2nd identi.ca account and add it as your friend + be friend of the 2nd account (so you can send direct messages)

4. Save this code and put it on your webpage (and change all lines where it says "change me"):

<?php
set_time_limit
(0);
ignore_user_abort(true); 

// connection to database
$con=mysql_connect("hostname","username","") OR DIE("Error");    // change me
$db=mysql_select_db("database",$con) OR DIE("Error");            // change me


function sentText($text,$user,$id,$from){
    
//
    // send message
    //
    
$username "username";        // change me - your 2nd identi.ca account (used to send tweets to your primary account)
    
$password "password";        // change me - password of your 2nd account
    
    
$url "http://identi.ca/api/direct_messages/new.xml";

    
$curl_handle curl_init();
    
curl_setopt($curl_handleCURLOPT_CONNECTTIMEOUT2);
    
curl_setopt($curl_handleCURLOPT_RETURNTRANSFER1);
    
curl_setopt($curl_handleCURLOPT_URL"$url");
    
curl_setopt($curl_handleCURLOPT_POST1);
    
curl_setopt($curl_handleCURLOPT_POSTFIELDS"source=$from&text=$text&user=$user");
    
curl_setopt($curl_handleCURLOPT_USERPWD"$username:$password");
    
$buffer curl_exec($curl_handle);
    
curl_close($curl_handle);

    if (empty(
$buffer)) {
        echo 
"error";
    } else {
        echo 
"sent tweet no ".$id."<br/>";
    }
}

function 
openFile($name){
    
//
    // connection to twitter
    //
    
$server="twitter.com";
    
$fp fsockopen ($server80$errno$errstr30);    
    if(!
$fp) {
        echo 
$errstr;
    } else {
        
$link "statuses/user_timeline/".$name.".xml";
        
fputs($fp"GET /".$link." HTTP/1.1\r\n");
        
fputs($fp"Host: ".server."\r\n");
        
fputs($fp"Connection: Close\r\n\r\n");
        
$data "";
        while (!
feof($fp)) {
            
$data.=fgets($fp);
        }
        
fclose($fp);
    }
     
$response=split("\r\n\r\n",$data);
     
$header=$response[0]; 
        
    if(!(
strpos($header,"Transfer-Encoding: chunked")===false)){
        
$aux=split("\r\n",$response[1]);
        for(
$i=0;$i<count($aux);$i++)
            if(
$i==|| ($i%2==0)) $aux[$i]="";
    }
    return 
$response[1];
}
    

function 
getArray(){
    
$q=mysql_query("SELECT * FROM twitter2identica");
    while (
$d=mysql_fetch_array($q)){
        
$last_index=0;
        
$fp=openFile($d["twitter_name"]);    //open rss file
        
$fppreg_replace("/([<<\/])([a-z0-9]+):/i","$1$2",$fp);
        
        
$xml= new SimpleXMLElement($fp);

        
$id=0;
        
$ar = array();
        
$ar2 = array();
        foreach (
$xml->status as $item){        
            
$id=(float)$item->id;
            
$text=(string)$item->text;
            
array_unshift($ar, array($id,$text));
        }
        
        
$last_index=$d["twitter_id"];
        
        if (
$last_index==0){
            
//new feed, just get the last tweeet
            
$new=array_pop($ar);
            
$ar=array();
            
array_push($ar, array($new[0],$new[1]));
        }
        
        foreach (
$ar as $item){
            if (
$item[0]>$last_index){
                
sentText($item[1],$d["twitter_to"],$item[0],$d["twitter_name"]);
                
$last_index=$item[0];
                
flush();
            }
        }
        
mysql_query("UPDATE twitter2identica SET twitter_id=".$last_index." WHERE twitter_to='".$d["twitter_to"]."' AND twitter_name='".$d["twitter_name"]."'");
    }
}

getArray();
?>


5. run a cronjob (e.g. use the free http://www.cronjob.de service) to call the script from above


Now you will receive a direct message to your main identi.ca account with the text of the twitter-tweet: