Chapter 6. API Call Examples

6.1. Java Script Examples
6.1.1. JSONP
6.1.2. Pure JavaScript Call
6.1.3. JQuery JSONP Call
6.2. PHP Examples
6.2.1. Caprice API PHP library

6.1. Java Script Examples

6.1.1. JSONP

Description

JSONP is a technic to overcome browser "one domain" restriction. (http://en.wikipedia.org/wiki/JSON#JSONP). So we are able to make AJAX calls to any domain. What is needed is basically add api_callback variable to GET call.

Example 6.1. JSONP API Call Example

http://api.mp3caprice.com/1.0/search/artists/10/Die+Fantastischen+Vier.json?api_key={API Key}&api_callback=myFunction 

myFunction(
{"apiResponse":{
"searchType":"ARTIST_CRITERION_KIND",
"resultsCount":1,
"searchString":"Die Fantastischen Vier",
"searchResults":[
  {"artistId":3729,
  "artistName":"Die Fantastischen Vier",
  "releasesCount":11,
  "tracksCount":202,
  "artistUrl":"http://mp3caprice.com/artist/die-fantastischen-vier-3729?pid=P_00101",
  "artistLargeCoverUrl":"http://cvr.mp3caprice.com/covers/artists/003/729/230.jpg",
  "artistSmallCoverUrl":"http://cvr.mp3caprice.com/covers/artists/003/729/90.jpg"
  }
  ]
})

6.1.2. Pure JavaScript Call

Description

What is needed is basically to pass an api_callback processing function name. Function will be executed with a response data as a parameter.

Example

Example 6.2. Pure JS JSONP API Call Request Example

<script>
function myFunction(data){
    alert(data);
}
</script>
<script src="http://api.mp3caprice.com/1.0/search/artists/10/Die+Fantastischen+Vier.json?api_key={API Key}&api_callback=myFunction"></script>

6.1.3. JQuery JSONP Call

Description

jQuery framework have native JSONP support.

Example

Example 6.3. jQuery JSONP API Call Request Example

<script>
$.ajax({
    url: 'http://api.mp3caprice.com/1.0/search/artists/10/Die+Fantastischen+Vier.json?api_key={API Key}',
    success: function(data){
       console.log(data)
    },
    jsonp: 'api_callback',
    dataType: 'jsonp'
});
</script>