Gratis Grooveshark API class
27 aug
Hej Alle, det er længe siden der har været noget aktivitet på siden, men nu er det tid til et lille indlæg igen. Jeg har for et par dage siden lavet en php klasse som gør at man nemt og hurtigt kan lave en applikation som samarbejder med Grooveshark.
Du kan downloade klassen her.
For at få url adressen på det første søgeresultat anvender du følgende metode:
getUrl(‘DIN SØGNING’);
For at modtage meta resultater for det første søgeresultat anvender du følgende metode:
getResult(‘DIN SØGNING’);
For at modtage meta informationer for helt op til 32 af søgeresultaterne anvender du denne metode, resultatet vil blive lageret som et php array.
getResults(‘DIN SØGNING’, ANTAL RESULTATER);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | <!--?php /* * Client class to communicate with the Grooveshark/Tiny song. * * Author: Gustav Svendsen * Website: www.gustavs.dk * Production time: 2011 * Requirements: TinySong Apikey, PHP and CURL extension */ class grooveshark { /* * The methods are defined below */ public $Apikey; private $_Apiurl = 'http://tinysong.com/'; protected $_ApiFormat; /* * The constructor sets the ApiKey and the response format */ public function __construct($Apikey, $ApiFormat = 'json') { //Defines the API key. $this--->Apikey = $Apikey; //Sets the format of the response $this->_ApiFormat = $ApiFormat; } /* * This functions returns only the link of the first search result */ public function getUrl($search = '') { //Checks if the input has been set if($search == '') { return false; } //Constructing the URl of the GET request $url = $this->_Apiurl . "a/" . urlencode($search) . "?format=" . $this->_ApiFormat . "&key=" . $this->Apikey; //Making the request $result = $this->curl($url); //Checks for valid result if($result == '[]') { return false; } //Returns the result of the request return $result; } /* * This functions return the number one search result with all the meta data */ public function getResult($search = '') { //Checks if the input has been set if($search == '') { return false; } //Constructing the url for the call $url = $this->_Apiurl . "b/" . urlencode($search) . "?format=" . $this->_ApiFormat . "&key=" . $this->Apikey; //Making the request $result = $this->curl($url); //Check if valid respond if($result == '[]') { return false; } //Converts the json object into a normal php array $output = json_decode($result, true); //Returns the result return $output; } /* * This function returns a number of search result, with the oppertunity to limit the output */ public function getResults($search = '', $limit = 32) { //Checks the input if($search == '') { return false; } //Constructing the url for the request $url = $this->_Apiurl . "s/" . urlencode($search) . "?format=" . $this->_ApiFormat . "&limit=" . $limit . "&key=" . $this->Apikey; //Making the requet $result = $this->curl($url); //Checks if valid respond if($result == '[]') { return false; } //Converts json opj to php array $output = json_decode($result, true); //Returns the result return $output; } /* * This functions makes all the curl calls to the API and returns it to the requesting function */ protected function curl($input) { //starts the curl session $cl = curl_init($input); //Sets the curl options curl_setopt($cl, CURLOPT_HEADER, 0); curl_setopt($cl, CURLOPT_AUTOREFERER, 1); curl_setopt($cl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0"); curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1); //Executes the curl call $return = curl_exec($cl); //Closes the curl session curl_close($cl); //Returns the value of the curl call return $return; } } |



Seneste Kommentarer