PDA

View Full Version : Last.fm "Add to mySpace" - how?!



Zaid_W1red
January 22nd, 2008, 01:12 PM
Hey Guys,

I've ben racking my head trying work work out how the bright spark at last.fm achieved the 'add to my myspace' function to their widgets!

Does anyone have any ideas?

I found the following PHP class: http://www.weberdev.com/get_example-4510.html - but the scripts old which is why I assume it doesn't work, and my attempts to contact the dev have been unsuccessful!

Appreciate anyones input :D

Zaid

mlk
January 22nd, 2008, 01:21 PM
have you tried posting on Last.fm's forums ? or contacting the php guys through their profile pages ?

Zaid_W1red
February 6th, 2008, 03:23 PM
have you tried posting on Last.fm's forums ? or contacting the php guys through their profile pages ?

Thanks for the reply!

Im not that familiar with their community - but I'll certainly try it now!

After trawling the web I pieced this together - it works, but its slow and pretty hit and miss.

Would any be interested In collaborating on this?

Zaid_W1red
February 6th, 2008, 10:32 PM
<?php
function extractText($pat1,$pat2,$page) {
$result = null;
//We explode the initial HTML
$result = @explode($pat1, $page);
//then we explode the "right" part of the html
$result = @explode($pat2, $result[1]);
//We get the "middle" part, whatever was between $pat1 and $pat2
$result = $result[0];
//We need to take out any line breaks
$result = ereg_replace("<br>", "", $result);
//take out any whitespace
$result = trim($result);
return $result;
}

$email = "";
$password = "";
$content = ""

$ch = curl_init();
$randnum = rand(1,9999999);
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookiejar-$randnum");
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookiejar-$randnum");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL,"http://www.myspace.com");
$page=curl_exec($ch) ;
// find the token
//
preg_match("/amp;MyToken=([^\"]+)\"/",$page,$token);
$token = $token[1];
//We have the token, log into MySpace.
//
// do login
//
curl_setopt($ch, CURLOPT_URL,"http://login.myspace.com/index.cfm?fuseaction=login.process&MyToken={$token}");
curl_setopt($ch, CURLOPT_REFERER, "http://www.myspace.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_POST, 1);
$postfields = "email=" . urlencode($email);
$postfields .= "&password=" . urlencode($password);
$postfields .= '&ctl00%24Main%24SplashDisplay%24login%24loginbutton .x=38&ctl00%24Main%24SplashDisplay%24login%24loginbutton .y=15';
curl_setopt($ch, CURLOPT_POSTFIELDS,$postfields);
$page = curl_exec($ch);
preg_match("/fuseaction=user&Mytoken=(.*)\"/",$page,$token);
$redirpage="http://home.myspace.com/index.cfm?fuseaction=user&MyToken=$token";
// do the redirect to user home page
curl_setopt($ch, CURLOPT_REFERER,"http://login.myspace.com/index.cfm?fuseaction=login.process&MyToken={$token}");
curl_setopt($ch, CURLOPT_URL,$redirpage);
curl_setopt($ch, CURLOPT_POST, 0);
$page = curl_exec($ch) ;
echo "loged in...";

//Once logged in, goto the edit profile page and post
// edit profile page
preg_match("/profile.interests&MyToken=(.*)\"/",$page,$token);
$token = $token[1];

curl_setopt($ch, CURLOPT_URL,"http://www.myspace.com/index.cfm?fuseaction=profile.interests&MyToken=MyToken={$token}");
curl_setopt($ch, CURLOPT_REFERER, "http://www.myspace.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_POST, 1);

$page = curl_exec($ch);

$hash = extractText('ctl00_ctl00_cpMain_ProfileEditContent _editInterests_hash" value="','"',$page);
$viewstate = extractText('__VIEWSTATE" value="','"',$page);
$currenttext = extractText('<textarea name="ctl00$ctl00$cpMain$ProfileEditContent$editInterest s$AboutMeTextBox" rows="15" cols="20" id="ctl00_ctl00_cpMain_ProfileEditContent_editInterest s_AboutMeTextBox" class="w500">','</text>',$page);
$currenttext = explode("</textarea>",$currenttext);


echo "About Box -->".$currenttext[0];

echo "<br/>Prefetched hash--> ".$hash."<br/>";
echo "<br/>Prefetched viewstate--> ".$viewstate."<br/>";
curl_setopt($ch, CURLOPT_URL,"http://www.myspace.com/index.cfm?fuseaction=profile.interests&MyToken=MyToken={$token}");
curl_setopt($ch, CURLOPT_REFERER, "http://www.myspace.com");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_POST, 1);
$postfields = "&__VIEWSTATE=$viewstate";

$postfields .= "&ctl00%24ctl00%24cpMain%24ProfileEditContent%24edit Interests%24hash=$hash";

$postfields .= "&ctl00%24ctl00%24cpMain%24ProfileEditContent%24edit Interests%24SaveInterests=Save All Changes";

//$postfields .= "&ctl00%24ctl00%24cpMain%24ProfileEditContent%24edit Interests%24LikeToMeetText=your mum";

$postfields .= "&ctl00%24ctl00%24cpMain%24ProfileEditContent%24edit Interests%24AboutMeTextBox=".$currenttext[0]."$content";

curl_setopt($ch, CURLOPT_POSTFIELDS,$postfields);
$page = curl_exec($ch);


if(preg_match("/Profile Updated/",$page)){
echo "done<br/><br/>";
exit;
}else{

echo "nope!";
}

curl_setopt($ch, CURLOPT_REFERER,"http://collect.myspace.com/index.cfm?fuseaction=signout");
curl_setopt($ch, CURLOPT_URL,$redirpage);
curl_setopt($ch, CURLOPT_POST, 0);
$page = curl_exec($ch) ;

print $page;
?>