Introducing the MySpace and MySpace Mobile PHP Classes

by Matt on December 12th, 2008

Update 2-26-09: After just testing my demo page, it looks like either one or both of the classes aren’t working. I don’t have any major motivations to fix them, because I’m too busy with other projects to deal with MySpace changing their HTML. I’m sure that the fix is easy… some pattern in one of the preg_matches probably needs a little tweaking. MySpace often changes the way they output their HTML, so I’m not surprised. If anyone has any interest in continuing development of these classes and wants to send me an update, I’m sure others would appreciate it. I will update more if I decide to crack this nut once again.

Update 12-20-08: The myspace class has been updated to reflect changes in the way MySpace outputs the HTML for friend listing pages. Download link is the same.

Presenting two MySpace classes:

  • MySpace Mobile
  • MySpace.com

programmed in PHP and released under the GPL. Download both classes here (myspace_classes.zip).

These classes provide basic profile and friend scraping functionality. My intention in programming them was to free personal & relationship data from MySpace for use in other applications. They were not created for spamming or other nefarious kinds of activities.

Each has their strengths and weaknesses. Used together, they can be very powerful. Here’s a brief breakdown:

MySpace Mobile

MySpace Mobile (located at m.myspace.com) is extremely useful for easily extracting user-entered profile information. Profile info is broken up into 6 categories:

  • Interests and Personality
  • Basic Info
  • Background and Lifestyle
  • Schools
  • Companies
  • Networking

“Basic Info” for instance, contains Gender, Occupation, City, Ethnicity, etc. It’s easy to extract this info from MySpace Mobile because it lays it out in easy-to-match HTML. Look at this DOM view of MySpace Mobile’s “Basic Info” section:

Talk about a breeze with preg_match…

For my purposes I found Interests & Personality, Basic Info and Background & Lifestyle to be the most interesting, and thus only included them as part of the profile parsing functionality. It wouldn’t be difficult to extend this to Schools, Companies and Networking.

Unfortunately MySpace Mobile is excrutiatingly slow compared to MySpace.com. In my tests, it takes about 0.5 seconds to grab a friend’s Basic Info. This might seem fast but that means grabbing 300 friends’ info would take ~150 seconds or 2 1/2 minutes. In fact it would normally take longer but I set up cURL to TIMEOUT after 2 seconds; in the event that a page takes longer than 2 seconds to load, the request will drop, and the class will try one more time to grab the page. This will sometimes result in pages dropped completely. The TIMEOUT value can be easily adjusted to your liking.

MySpace

The MySpace class is primarily useful for quickly grabbing all of a user’s friends’ rudimentary info: Profile ID, Main Image, and Name. In my tests, grabbing between 300 and 400 friends took about 2 seconds. Grabbing 70 friends took 1.5 seconds.

“Grabbing” friends means the class will return a 2D array of your friends’ info. In this example:

$test = new myspace();
$test->login("username", "password");
$my_friends = $test->grabFriendBasics($test->myID);

$my_friends will be an array with the following indices: ['name'], ['img_url'] and ['id']. They each will contain the same number of values, e.g. if you have 70 friends then name, img_url and id will all have 70 values each, in the order in which your friends are displayed on your friend listing pages.

Together

I have found the most interesting utility in using the MySpace class to grab all rudimentary friend info, and then using those friend IDs to grab all demographic info with MySpace Mobile. Here’s an example:

$rud = new myspace();
$rud->login("username", "password");
$my_friends = $rud->grabFriendBasics($rud->myID);

$msm = new myspace_m();
$msm->login("username", "password");
for($i = 0; $i < count($my_friends['id']); $i++) {
     $details = $msm->getFriendDetails($my_friends['id'][$i], array("basic"));
     // If first iteration, output the structure of the Basic info listing
     if($i == 0) {
          print_r($details['basic_def']);
     }
     print_r($details['basic']);
}

Of course, I don’t expect these classes to necessarily be used for what I’ve set them up for. If you have any feature requests, leave a comment below.

For a live demonstration of simple functionality, along with more code examples, check out the Demo.

2 Comments
  1. Abhijit permalink

    Here we get the friend id and the display name of friends. But I need the email ids of the friends/contacts. Can any body please help me out. I will be greatful

  2. Matt permalink

    Hi Abhijit,

    MySpace doesn’t allow reverse lookup of e-mail addresses given a user ID. However, you can search for a user by e-mail address and get their user ID. I’m not sure if the current class is capable of handling that, but I’ll put in that functionality soon and update here when I do. Ultimately, I don’t think there is any way to do what you’re looking for.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS