#!/usr/bin/env php
<?php

if (!file_exists('creds.php')) {
  echo 
"You need to create a file creds.php to include the following:\n"
  
"<?php\n"
  
" define('USERNAME','your-username');\n"
  
" define('PASSWORD','your-password');\n"
  
"?>\n";
  exit(
1);
 }

include 
'creds.php';
$username USERNAME;
$password PASSWORD;

function 
makeRequest($username,$password,$page=1) {
  
$s curl_init();
  
$header = array();
  
$header []= 'Authorization: Basic ' base64_encode($username ':' $password);
  
curl_setopt($s,CURLOPT_URL,'http://twitter.com/statuses/friends_timeline.xml?page=' $page); 
  
curl_setopt($s,CURLOPT_HTTPHEADER,$header);
  
curl_setopt($sCURLOPT_CONNECTTIMEOUT30);
  
curl_setopt($sCURLOPT_TIMEOUT30);
  
curl_setopt($sCURLOPT_RETURNTRANSFER1);
  
$res curl_exec($s);
  
curl_close($s);
  return 
$res;
}

function 
month2number($i) {
  if      (
$i=="Jan") return "01";
  else if (
$i=="Feb") return "02";
  else if (
$i=="Mar") return "03";
  else if (
$i=="Apr") return "04";
  else if (
$i=="May") return "05";
  else if (
$i=="Jun") return "06";
  else if (
$i=="Jul") return "07";
  else if (
$i=="Aug") return "08";
  else if (
$i=="Sep") return "09";
  else if (
$i=="Oct") return "10";
  else if (
$i=="Nov") return "11";
  else if (
$i=="Dec") return "12";
}

function 
parse($res) {
  
$lines preg_split('/\n/',$res);
  
$in_users FALSE;
  
$cnt 0;
  foreach (
$lines as $line) {
    if (!
$in_users) {
      if (
preg_match('/<user>/',$line)) {
        
$in_users TRUE;
      }
    } else {
      if (
preg_match('/<\/user>/',$line)) {
        
$in_users FALSE;
      }
    }
    if (
$in_users) {
      if (
preg_match('/<screen_name>([^<]+)<\/screen_name>/',$line,$out)) {
        
$id $out[1];
        echo 
$id "\t" $date "\n";
        
$cnt++;
      }
    } else {
      if (
preg_match('/<created_at>\w+ (\w+) (\d\d) \w\w:\w\w:\w\w.*(\d{4})<\/created_at>/',$line,$out)) {
          
$month month2number($out[1]);
          
$day $out[2];
          
$year $out[3];
          
$date $year $month $day;
      }
    }
  }
  return 
$cnt>0;
}

for (
$page 1;;$page++) {
  if (!
parse(makeRequest($username,$password,$page))) break;
 }
?>