1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106:
| <?php
class twitter {
public $user; public $pass; public $seperator = ' | '; public $msg; private $modules;
function __construct( $user, $pass ) { $this->modules_load(); $this->user = $user; $this->pass = $pass; }
function update( $msg = '' ) { $string = implode( $this->seperator, $this->msg ); if( strlen( $string ) > 140 ) { $strings = str_split( $string, 140 ); foreach( $strings as $string ) { $this->send( $string ); } } else $this->send( $string ); } function send( $string ) { if( ( strlen( $string ) <= 140 ) && !empty( $this->user ) && !empty( $this->pass ) ) { $s = curl_init(); curl_setopt( $s, CURLOPT_URL, 'http://twitter.com/statuses/update.xml' ); curl_setopt( $s, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ); curl_setopt( $s, CURLOPT_HEADER, false ); curl_setopt( $s, CURLOPT_USERPWD, $this->user.':'.$this->pass ); curl_setopt( $s, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $s, CURLOPT_POST, 1 ); curl_setopt( $s, CURLOPT_POSTFIELDS, 'status='.urlencode( utf8_encode( $string ) ) ); $result = curl_exec( $s ); curl_close( $s ); if( $result != 'Could not authenticate you' ) { if( strpos( $result, '<created_at>' ) !== false ) { return true; } else echo $result; } } return false; }
function modules_load() { $files = scandir( '.' ); foreach( $files as $file ) { if( preg_match( '/^twitter\.module\.(.+?)\.php$/', $file, $match ) ) { $this->modules[$match[1]] = $this->module_load( $match ); } } }
function module_load( $name ) { require_once( $name[0] ); $name = 'module_'.$name[1]; return new $name; }
function modules_exec( $func ) { foreach( $this->modules as $module ) { $this->msg[] = $module->$func(); } }
}
// example: set class; load all modules; exec the function get() in all modules; // and send the result to twitter; /* $t = new twitter( 'twitteruser', 'twitterpass' ); $t->modules_exec( 'get' ); $t->update(); */
// example: set class; update your twitter site with a small text /* $t = new twitter( 'twitteruser', 'twitterpass' ); $t->send( 'that\'s funny!' ); */
// example: use it as instant messanger for your homepage /* <?php
if( $_POST['submit'] && !empty( $_POST['name'] ) && !empty( $_POST['text'] ) ) { $t = new twitter( 'twitteruser', 'twitterpass' ); $msg = $_POST['name'].' wrote: '.$_POST['text']; $t->send( $msg ); echo 'msg has been send! thx :D'; } else { ?> <form action="" method="post"> Name: <input type="text" name="name" value="<?php echo $_POST['name']; ?>" maxsize="12" /><br /> Text: <input type="text" name="text" value="<?php echo $_POST['text']; ?>" maxlength="120" /><br /> <input type="submit" name="submit" value="send" /> <?php } ?>
*/
?>
|