$val) { fwrite($oh,"param: $key - $val\n"); } // construct the URL $url = "http://developer.echonest.com/api/v4/{$api}?"; // concatenate key=>value pairs from params, delimit with '&' foreach ($params as $key=>$value) { if (is_array($value)) { // nested array: probably description foreach ($value as $nkey => $nval) { if (is_array($nval)) { foreach ($nval as $lnval) { //$url .= '&' . urlencode($key) . '=' . urlencode($nkey) . ':' . urlencode($lnval); if (strcmp($nkey,'style') == 0) { $url .= '&' . $key . '=' . $nkey . '^' . $lnval; } else { $url .= '&' . $key . '=' . $nkey . ':' . $lnval; } } } else { if (empty($nval)) { //$url .= '&' . urlencode($key) . '=' . urlencode($nkey); $url .= '&' . $key . '=' . $nkey; } else { //$url .= '&' . urlencode($key) . '=' . urlencode($nkey) . ':' . urlencode($nval); if (strcmp($key,'style') == 0) { $url .= '&' . $key . '=' . $nkey . '^' . $nval; } else { $url .= '&' . $key . '=' . $nkey . ':' . $nval; } } } } } else { //$url .= '&' . urlencode($key) . '=' . urlencode($value); $url .= '&' . $key . '=' . $value; } } // get the playlist fwrite($oh,"url: $url\n"); $json_obj = file_get_contents($url); // check status if (is_bool($json_obj)) { fwrite($oh,"error fetching url\n"); return ''; } else { fwrite($oh,"json: {$json_obj}\n"); $jdata = json_decode($json_obj); } $jstat = $jdata->{'response'}->{'status'}; if ($jstat->{'code'} > 0) { fwrite($oh,"error: {$jstat->{'message'}}\n"); die('ERROR: ' . $jstat->{'message'}); } fclose($oh); return $jdata->{'response'}; } // this function uses http 'POST' to interface with EchoNest function post_echonest_data($api,$params) { $url = "http://developer.echonest.com/api/v4/$api"; $ch = curl_init($url); curl_setopt($ch,CURLOPT_POST,1); if (!empty($params)) { // $postflds = ''; //foreach ($params as $key=>$value){ // $postflds .= urlencode($key) . '=' . urlencode($value) . '&'; //} //$postflds = substr($postflds,0,strlen($postflds)-1); //curl_setopt($ch,CURLOPT_POSTFIELDS,$postflds); curl_setopt($ch,CURLOPT_POSTFIELDS,$params); //print "post fields: $postflds\n"; } curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); $data = curl_exec($ch); curl_close($ch); $jdata = json_decode($data); return $jdata->{'response'}; } // this function creates and/or finds the id for an EchoNest catalog // must define: $params['api_key'], $params['type'], $params['name'] function get_echonest_catalog($params) { $catdata = post_echonest_data('catalog/create',$params); $statcode = $catdata->{'status'}->{'code'}; if ($statcode) { print $catdata->{'status'}->{'message'} . "\n\n"; print "Trying to fetch a current catalog ID\n"; // fetch current catalog ID for the given name unset($params['type']); $data = get_echonest_data('catalog/profile',$params); $statcode = $data->{'status'}->{'code'}; if ($statcode) { print $data->{'status'}->{'message'} . "\n\n"; return; } else { $en_cat_id = $data->{'catalog'}->{'id'}; } } else { $en_cat_id = $catdata->{'id'}; } return $en_cat_id; }