Create Affiliate ID
Code samples below do not contain a valid Affiliate ID. Create an Affiliate account before using these.
Real sample code:
- PHP class to use our webservice View and download
- Top 10 View in action and download
- Search View in action and download
- Artist List View in action and download
Copied!
<?php
class KVService
{
/**
* Replace with your affiliateId and the service URL you want to use.
*/
const AFFILIATE_ID = 77;
const SERVICE_URL = 'http://www.karaoke-version.com/api/';
/**
* Query the webservice using curl
* @param string $fullQuery
* @return string Webservice response
*/
private static function _curlQuery($fullQuery)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fullQuery);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$jsonResultStr = curl_exec($ch);
curl_close($ch);
return $jsonResultStr;
}
/**
* Query the webservice using standard PHP function (file_get_contents)
* @param string $fullQuery
* @return string Webservice response
*/
private static function _standardQuery($fullQuery)
{
return @file_get_contents($fullQuery);
}
/**
* Get the full query URL from the given kind and query
* @param string $kind
* @param array $query
* @return string Full query URL
*/
public static function getFullQueryUrl($kind, $query)
{
if (!isset($query["affiliateId"])) {
$query["affiliateId"] = self::AFFILIATE_ID;
}
$jsonQuery = json_encode($query);
return self::SERVICE_URL . $kind . '/?query=' . urlencode($jsonQuery);
}
/**
* Query the webservice
* @param string $kind The action kind (artist, song, songfile, search)
* @param array $query Your query
* @return stdClass Webservice response.
* @throws Exception If the webservice encountered an error
*/
public static function query($kind, $query)
{
$fullQuery = static::getFullQueryUrl($kind, $query);
$jsonResultStr = null;
if (in_array('curl', get_loaded_extensions())) {
$jsonResultStr = self::_curlQuery($fullQuery);
} else {
$jsonResultStr = self::_standardQuery($fullQuery);
}
$result = json_decode($jsonResultStr);
if (isset($result->error)) {
throw new Exception("KV service error [" . $result->error->code . "]: " . $result->error->message);
}
return $result;
}
/**
* Shortcut for KVService::query('artist', $query)
* @param array $query
* @return stdClass
* @throws Exception
*/
public static function queryArtist($query)
{
return static::query('artist', $query);
}
/**
* Shortcut for KVService::query('song', $query)
* @param array $query
* @return stdClass
* @throws Exception
*/
public static function querySong($query)
{
return static::query('song', $query);
}
/**
* Shortcut for KVService::query('songfile', $query)
* @param array $query
* @return stdClass
* @throws Exception
*/
public static function querySongFile($query)
{
return static::query('songfile', $query);
}
/**
* Shortcut for KVService::query('search', $query)
* @param array $query
* @return stdClass
* @throws Exception
*/
public static function querySearch($query)
{
return static::query('search', $query);
}
}