1

I am trying to upload a file (input type and named "upload") through the echonest api but am struggling to get the curl upload working.

http://developer.echonest.com/docs/v4/track.html#upload

Can anybody point me into the right direction to get the data-binary and header: application/octet-stream POST method working. This A HTTP POST request with Content-Type "application/octet-stream", with the local file as the body of the request, and the parameters in the URL.

the curl php:

$localfile = $_FILES['upload']['name'];
$fp         = fopen($localfile, 'r');
$post       = array('$localfile'=>'@$localfile','api_key'=>'xx','filetype'=>'mp3');
$ch         = curl_init();

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); // --data-binary
curl_setopt($ch, CURLOPT_URL, 'http://developer.echonest.com/api/v4/track/upload');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-type: application/octet-stream');

$response   = curl_exec($ch);
$result     = json_decode($response,true);
$id         = $result['response']['track']['id'];

curl_close($ch);
fclose($fp);

echo $id;
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
LJM JR
  • 43
  • 1
  • 7

1 Answers1

0

The problem might have something to do with your use of variables inside a single-quoted string. Also, improper use of the @ symbol. Try this...

array("$localfile"=>"$localfile",'api_key'=>'xx','filetype'=>'mp3');

Though that doesn't look right either. You probably want this..

array("localfile"=>"$localfile",'api_key'=>'xx','filetype'=>'mp3');
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116