Howdy folks,
I had to figure out how to login to Myspace using curl and PHP for a project. So I thought I would post some code here for anyone looking to do something similar.
$cookieFile = ‘/tmp/mscookiejar-’.time().$randnum = mt_rand(11111, 999999);
$msUsername = ‘MYSPACEUSERNAME’;
$msPassword = ‘MYSPACEPASSWORD’;
//
// Log user into myspace
//
$curl_connection = curl_init(’https://secure.myspace.com/index.cfm?fuseaction=login.process’);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, 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($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($curl_connection, CURLOPT_COOKIEFILE, $cookieFile);
//curl_setopt($curl_connection, CURLOPT_VERBOSE, true);
curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_REFERER, “http://www.myspace.com”);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, Array(”Content-Type: application/x-www-form-urlencoded”));
$post_string = urlencode(’ctl00$ctl00$cpMain$cpMain$LoginBox$SingleSignOnHash’).’=';
$post_string .= ‘&’.urlencode(’ctl00$ctl00$cpMain$cpMain$LoginBox$SingleSignOnRequestUri’).’=';
$post_string .= ‘&’.urlencode(’ctl00$ctl00$cpMain$cpMain$LoginBox$nexturl’).’=';
$post_string .= ‘&’.urlencode(’ctl00$ctl00$cpMain$cpMain$LoginBox$apikey’).’=';
$post_string .= ‘&’.urlencode(’ctl00$ctl00$cpMain$cpMain$LoginBox$ContainerPage’).’=';
$post_string .= ‘&’.urlencode(’ctl00$ctl00$cpMain$cpMain$LoginBox$SMSVerifiedCookieToken’).’=';
$post_string .= ‘&’.urlencode(’ctl00$ctl00$cpMain$cpMain$LoginBox$Remember_Checkbox’).’=off’;
$post_string .= ‘&__VIEWSTATE=/wEPDwULLTIwOTI5NDgxMzlkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBTRjdGwwMCRjdGwwMCRjcE1haW4kY3BNYWluJExvZ2luQm94JFJlbWVtYmVyX0NoZWNrYm94′;
$post_string .= ‘&NextPage=’;
$post_string .= ‘&’.urlencode(’ctl00$ctl00$cpMain$cpMain$LoginBox$Email_Textbox’).’=’.urlencode($msUsername);
$post_string .= ‘&’.urlencode(’ctl00$ctl00$cpMain$cpMain$LoginBox$Password_Textbox’).’=’.urlencode($msPassword);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($curl_connection);
curl_close($curl_connection);
// Find user id and name
$msIDStart = strpos($result, ‘{”UserId”:’) + 10;
$msIDEnd = strpos($result, ‘,”DisplayFriendId”:’) - $msIDStart;
$msID = substr($result, $msIDStart, $msIDEnd);
$msNameStart = strpos($result, ‘var uName = “‘) + 13;
$msNameEnd = strpos($result, ‘if(uName.length>20){’) - 13 - $msNameStart;
$msName = substr($result, $msNameStart, $msNameEnd);