Hi,
heute mal wieder ein kleines aber evtl. fuer dich hilfreiches Script. Dieses mal geht es darum die Nachrichten einer sogenannten Shoutbox, wie sie z.B. auf der Seite meines Jugentreffs benutzt wird per SMS aufs Handy zubekommen. Ich nutze dazu einfach Twitter. Aber seht selbst:
<?php // Created 2007 by andi@saerdnaer.de // Licenced under GPLv2 setlocale(LC_ALL, 'de_DE@euro', 'de_DE', 'de', 'ge'); //$path = '/home/user/dir/'; $path = dirname(__FILE__); function post_to_twitter($username, $password, $message) { // The twitter API address $url = 'http://twitter.com/statuses/update.xml'; // Alternative JSON version // $url = 'http://twitter.com/statuses/update.json'; // Set up and execute the curl process $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, "$url"); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handle, CURLOPT_POST, 1); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message"); curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password"); $buffer = curl_exec($curl_handle); curl_close($curl_handle); // check for success or failure return !empty($buffer); } $url = 'http://1234567.shoutbox.de/'; $twitter_username = 'nutzername'; $twitter_password = 'passwort'; $file = implode('', file($url)); $file = utf8_encode($file); $file = preg_replace('#<img[^>]+>#is', '', $file); $file = preg_replace('#<a[^>]+HREF="./go/\?u=([^"]+)"[^>]+>(?:.+?)</a>#is', '\1', $file); if ( !preg_match_all('#<table (?:.+?)<font class="SB_shoutbox">([0-9\.]+) ([0-9\.]+)</font></div><b>(.+?)</b>:<br>(.+?)<script>(?:.+?)</table>#is', $file, $regs)) die('No entry matches!'); $i = count($regs[0])-1; $latest_time = strtotime($regs[1][$i] . " " . $regs[2][$i]); $last_update_time = intval(implode('', file($path.'last_update_time'))); if ( $latest_time > $last_update_time ) { $fp = fopen($path.'last_update_time', 'w'); fwrite($fp, $latest_time); fclose($fp); } else { exit; } for ( $i = 0; $i < count($regs[0]); $i++ ) { $item_time = strtotime($regs[1][$i] . " " . $regs[2][$i]); if ( $item_time <= $last_update_time ) { continue; } $item_author = $regs[3][$i]; $item_message = $regs[4][$i]; $item_date = date('G:i', $item_time); post_to_twitter($twitter_username, $twitter_password, $item_author . ' ' . $item_date . ': ' . $item_message); } ?>
URL zur Shootbox, Twitter Username und Passwort muessen natuerlich angepasst werden. Ich lasse das Script via Cronjob alle 5 Minuten laufen.
Kommentar verfassen