Use your left/right keys to browse tutorials
MySpace Open Search API

MySpace Open Search API

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on July 4, 2010

The MySpace Search API allows you to search MySpace for user, videos, images, and music. With an individual feed for each search item. It is designed to function like a search on the MySpace website and to have a response format that is compatible with OpenSocial. The API returns either JSON or XML and in this tutorial i’ll be using PHP and CURL to parse an XML response to build a simple MySpace search page.

Application use

Open Search could be used to mash data using the MySpace API with other applications, for example automatically pulling in a users MySpace profile image, name, location and URL. Building a Facebook application that recommends bands to you on MySpace.

The Code

Just like the Twitter search API tutorial that I did a last year we’re using the CURL function in PHP5 to allow us to parse the XML file and format the data that we get back.

Here’s how it works

Any search that we make will send GET variables through the page URL therefore making it easy to see the data that we’re sending to the API generally you will see search pages use this method as we’re not transmitting private data such as usernames or passwords.

The search data is stored in the variable $q if nothing is searched for initially we run a default search mainly for demo purposes with the search term ‘Coldplay’. You’ll notice that the first result on the left is highlighted green, this is because it is the official profile for the Coldplay. you can see that we construct the URL and store it in the variable $search. There are many different variables that can be passed to the API. It’s worth pointing out that we’re using the People search call. You can see all the values that can be passed in here

Here is an example XML request: http://api.myspace.com/opensearch/people?searchTerms=ferrari&format=xml


//Search API Script

$q=$_GET['q'];

if($_GET['q']==''){

$q = 'coldplay';

}

if($_GET['pics']==1){

$pics[0] = '&hasPhoto=on';
$pics[1] = 'checked';

}

if(!empty($_GET['location'])){

$pics[0] = '&location='.urlencode($_GET['location']).'';

}

$search = "http://api.myspace.com/opensearch/people?searchTerms=".urlencode($q)."&format=xml".$pics[0]."";

$cu = curl_init();

curl_setopt($cu, CURLOPT_URL, $search);
curl_setopt($cu, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($cu);
$search_res = new SimpleXMLElement($result);

echo "<h3>".$search_res->totalResults." MySpace results for '".$q."'</h3>";

// Echo the Search Data

foreach ($search_res->entry as $user) {

$officialprofile = null;

if($user->isOfficial==1){

$officialprofile = 'official';

}

echo "<div class='user ".$officialprofile."'><a href=\"".$user->profileUrl."\" target=\"_blank\"><img border=\"0\" width=\"68\" class=\"user_image\" src=\"".$user->thumbnailUrl."\" title=\"".$user->displayName."\" /></a>";
echo "<div class='text'>".$user->displayName."</div> <strong>".$user->location."</strong><br/><a href='".$user->profileUrl."' >Visit ".$officialprofile." profile</a><div class='clear'></div></div>";

}

curl_close($cu);

<div id="search">
<form action="" method="get">
  Search MySpace <input type="text" name="q" value="<?php echo $q;?>" id="searchbox" />
 Location <input type="text" name="location" value="<?php echo $_GET['location'];?>" id="location" />
  Only show users with profile pics <input name="pics" type="checkbox" value="1" <?php echo $pics[1];?> />
  <input type="submit" name="submit" id="submit" value="Search" />

</form>
</div>



More tutorials from Papermashup
Comments
3 discussions around MySpace Open Search API
  1. It is in reality a great and helpful piece of info. I am satisfied that you just shared this useful information with us. Please keep us informed like this. Thank you for sharing.

  2. Nafun says:

    Although it may be pretty quiet now, I would still like to integrate myspace on this page:

    http://watchconcerts.ca/downloads.php

    Does the Myspace API allow posting via twitter?

  3. Bakul Chandra Muni says:

    It does not work.
    I get the error.

    Fatal error: Uncaught exception ‘Exception’ with message ‘String could not be parsed as XML’ in /nfs/c02/h11/mnt/40992/domains/papermashup.com/html/demos/myspace-open-search/index.php:110 Stack trace: #0 /nfs/c02/h11/mnt/40992/domains/papermashup.com/html/demos/myspace-open-search/index.php(110): SimpleXMLElement->__construct(”) #1 {main} thrown in /nfs/c02/h11/mnt/40992/domains/papermashup.com/html/demos/myspace-open-search/index.php on line 110





Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in contact. If your question is related to a free script download, please use the comments on the article page as community members are more likely to respond quicker than I can personally.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com please get in touch via the contact link below for advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.