6.2. PHP Examples

6.2.1. Caprice API PHP library

6.2.1. Caprice API PHP library

Description

We have prepared PHP wrapper class to simplfy API calls from PHP code.

All what you need is to create CapriceApi.class.php file, Create CapriceApi PHP Wrapper instance (don't forget to define your {API Key}!) and easily Make PHP API calls.

Example 6.4. CapriceApi PHP wrapper class (CapriceApi.class.php)

<?php
class CapriceApi {

    private $host;
    private $key;

    private function apiCall($url) {
        $url = $this->host.$url.'.json?api_key='.$this->key;
        $r = json_decode(file_get_contents($url));
        return $r->apiResponse;
    }
    
    private function search($type, $condition, $maxCount) {
        return $this->apiCall('search/'.$type.'/'.$maxCount.'/'.urlencode($condition) );
    }

    /* PUBLIC */

    public function  __construct(Array $configuration) {
        $this->host = $configuration['host'];
        $this->key = $configuration['key'];
    }

    public function artistById($artistId) {
        return $this->apiCall('artist/byId/'.$artistId);
    }

    public function artistByLetter($letter, $pageNumber, $pageSize) {
        return $this->apiCall('artist/byLetter/'.$pageSize.'/'.$pageNumber.'/'.$letter);
    }

    public function artistByName($name) {
        return $this->apiCall('artist/byName/'.urlencode($name));
    }

    public function artistTop() {
        return $this->apiCall('artist/top');
    }

    public function releaseById($releaseId) {
        return $this->apiCall('release/byId/'.$releaseId);
    }

    public function releaseByType($type, $pageNumber, $pageSize) {
        return $this->apiCall('release/byType/'.$pageSize.'/'.$pageNumber.'/'.$type);
    }

    public function releaseByArtist($artistId, $type) {
        return $this->apiCall('release/byArtist/'.$type.'/'.$artistId);
    }

    public function releaseByGenre($genreId, $pageNumber, $pageSize) {
        return $this->apiCall('release/byGenre/'.$pageSize.'/'.$pageNumber.'/'.$genreId);
    }

    public function releaseByAddedDate($startDate, $endDate, $pageNumber, $pageSize) {
        return $this->apiCall('release/byAddedDate/'.$pageSize.'/'.$pageNumber.'/'.$startDate.'/'.$endDate);
    }

    public function releaseBySeries($serieId) {
        return $this->apiCall('release/bySeries/'.$serieId);
    }

    public function releaseTop($chartType, $maxCount) {
        return $this->apiCall('chart/'.$maxCount.'/'.$chartType);
    }

    public function genreById($genreId) {
        return $this->apiCall('genre/byId/'.$genreId);
    }
    public function genreByArtist($artistId) {
        return $this->apiCall('genre/byArtist/'.$artistId);
    }
    public function genreByReleaseCount($releaseCount) {
        return $this->apiCall('genre/all/'.$releaseCount);
    }

    public function searchArtists($condition, $maxCount) {
        return $this->search('artists', $condition, $maxCount);
    }

    public function searchReleases($condition, $maxCount) {
        return $this->search('releases', $condition, $maxCount);
    }

    public function searchSongs($condition, $maxCount) {
        return $this->search('songs', $condition, $maxCount);
    }

    public function seriesRoot() {
        return $this->apiCall('series/root');
    }

    public function seriesByParentId($serieId) {
        return $this->apiCall('series/byId/'.$serieId);
    }

}

Example 6.5. Create CapriceApi PHP Wrapper

<?php
require_once('CapriceApi.class.php');
$ds = new CapriceApi(array(
    'host'      => 'http://api.mp3caprice.com/1.0/',
    'key'      => '{API Key}'
));

Example 6.6. CapriceApi PHP Call Examples

/* Get Artist(s) */

$ds->artistById(610)

$ds->artistByLetter('A', 1, 2)

$ds->artistByName('The Doors')

/* Get Release(s) */

$ds->releaseById(12729)

$ds->releaseByType('Album', 1, 2)

$ds->releaseByArtist(610, 'Artist_Compilation')

$ds->releaseByGenre(1029, 1, 2)

$ds->releaseBySeries(62)

$ds->releaseByAddedDate('2010-08-20', date('Y-m-d'), 1, 2) 

/* Get Series(s) */

$ds->seriesRoot()

$ds->seriesByParentId(12)

/* Get Genre(s) */

$ds->genreById(1029)

$ds->genreByArtist(8840)

/* Execute searches */

$ds->searchArtists('Elian Elias', 10)

$ds->searchReleases('album', 10)

$ds->searchSongs('track', 10)