Description:
PHP script to post what your listening to to Twitter. It will only post the album when it changes.
Due to the last.fm api it's a bit ropey because the results are inconsistent. The twitter code was taken from the twitter api examples.
You'll need a twitter api key a Last.fm account. You can follow me on twitter @digital_mike
<?php //Last.fm to twitter - posts current album to twitter //Version 0.75 //Bug:Unknown Album is firing WAY too often, not writing to text file //Bug:Cutting of of data (album, dred = dredg) //constants $twitter_username = 'TWITTER ACCOUNT NAME'; $twitter_password = 'TWITTER PASSWORD'; $errno = 0; $errstr = ''; $response = ''; function httpRequest($host, $path = '/', $method = 'GET') { $header = "$method $path HTTP/1.1\r\n"; $header .= "Host: $host\r\n"; $header .= "Accept-Encoding: none\r\n"; $header .= "Authorization: Basic " . base64_encode("{$twitter_username}:{$twitter_password}") . "\r\n"; $header .= "Connection: Close\r\n\r\n"; if (!$sock) { } else { } return true; } } $count = '0'; //function to get lastfm data function getLastfmdata() { $lastfm_username = "LASTFM USERNAME"; $lastfm_apikey = 'LASTFM API KEY'; $myFile = "data.txt"; $lfm = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=".$lastfm_username."&api_key=".$lastfm_apikey); //get the data and write to variables and text doc foreach ($lfm->recenttracks as $recent) { foreach ($recent->track as $track) { $artist = $track->artist; $album = $track->album; //gets data //get old data //set flag if different //write new data if different $Falbum = $finalAlbum; $Fartist = $finalArtist; if ($finalAlbum == $album) { $flag = '1'; } $count = $count + 1; // count so it stops after one $stringData = $album; if ($album == "") { $stringData = "Unkown Album"; } $stringData = "#-#"; $stringData = $artist; $LFalbum = $artist; if ($count == "1") { // stop after the first record break; } } } }; getLastfmdata(); $myFile = "data.txt"; if ($flag != '1') { $finaltwitter = "Listening to: ".$finalAlbum." by ".$finalArtist; httpRequest('twitter.com', "/statuses/update.xml?status=$finaltwitter3", 'POST'); } else { } ?>





