#!/usr/bin/php
<?php

// A simple function using Curl to post (GET) to Twitter
// Kosso : March 14 2007

// Feel free to do what you like with this.
// It's pretty easy. But I thought I'd just put it out there.
// Curl is your friend. 

// usage :  postToTwitter("myusername","mypassword","hello twitterati! I'm posting this from a PHP script! woo!");

require_once '/usr/local/minerva/conf/twitter.inc';

/////////////////////////////////////////////////////////////////////

$user = $argv[1];
if ($user == "default") {
  $user = getDefaultUser();
}

$twitUser = getTwitterUser($user);
$twitPass = getTwitterPass($user);

$all = "";
for($i=2; $i < $argc;++$i) {
   $all .= $argv[$i]." ";
}

file_put_contents("/var/log/minerva/bearskin/tweet", $argv[0]." $user $all\n", FILE_APPEND);
exit(postToTwitter($twitUser, $twitPass, $all));


function postToTwitter($username,$password,$message){

    // GET the API url via web authentication
    // add 'status' param to send a message to Twitter

    $host = "http://twitter.com/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $host);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_POST, 1);

    // Go for it!!!
    $result = curl_exec($ch);
    // Look at the returned header
    $resultArray = curl_getinfo($ch);

    // close curl
    curl_close($ch);

//    echo "http code: ".$resultArray['http_code']."<br />";

    if($resultArray['http_code'] == "200"){
       return 0;
    } else {
       return 1;
    }

    // debug the result
    // echo "<pre>";
    // print_r($resultArray);
    // echo "</pre><hr>";

    // $sResult = htmlentities($result);
    // $sResult = str_replace("&gt;&lt;","&gt;<br />&lt;",$sResult);

    // echo "<pre>";
    // print $sResult;
    // echo "</pre>";

}


// ok, you can stop reading now










// no, seriously. ;p

?>
