api - Upload track on SoundCloud using PHP -


i trying upload track using library.

https://github.com/mptre/php-soundcloud 

other services authentication, getting access token, creating playlist working fine when trying upload track it's failed , return code [0,422,500] in several cases in tried.

case 1 : failed

$file = file_get_contents('sound-1.mp3'); $response = $client->post('tracks', array("track[title]"=>"track 1", "track[asset_data]"=>$file), array(curlopt_httpheader=>array("content-type: multipart/form-data"))); 

case 2 : failed

$file = base64_encode(file_get_contents('sound-1.mp3')); //binary format $response = $client->post('tracks', array("track[title]"=>"track 1", "track[asset_data]"=>$file), array(curlopt_httpheader=>array("content-type: multipart/form-data"))); 

case 3 : failed

$file = new curlfile(sound-1.mp3'); $response = $client->post('tracks', array("track[title]"=>"track 1", "track[asset_data]"=>$file), array(curlopt_httpheader=>array("content-type: multipart/form-data"))); 

case 4 : failed

$file = "@sound-1.mp3"; $response = $client->post('tracks', array("track[title]"=>"track 1", "track[asset_data]"=>$file), array(curlopt_httpheader=>array("content-type: multipart/form-data"))); 

it's showing deprecation method use '@' file used curlfile class method handle file curl.

please let me know if mistake side. note set access token request header there no error authorization. there may silly mistake data send soundcloud.

you can call soundcloud http api directly without wrapper. please see code sample below:

<?php //app credentials. create 1 @ http://soundcloud.com/you/apps define('api_url', 'https://api.soundcloud.com'); define('client_id', ''); define('client_secret', '');  //user credentials define('email', ''); define('password', '');  //path file upload define('file', 'mpthreetest.mp3');  class soundcloudapi {     private $url;     private $clientid;     private $secret;     private $accesstoken;      public function __construct($url, $clientid, $secret) {         $this->url = $url;         $this->clientid = $clientid;         $this->secret = $secret;     }      public function auth($username, $password) {         $url = $this->url . '/oauth2/token';         $data = array(             'client_id' => $this->clientid,             'client_secret' => $this->secret,             'grant_type' => 'password',             'username' => $username,             'password' => $password         );          $result = $this->request($url, $data, 'post');         $this->accesstoken = $result->access_token;         return $result;     }      public function upload($title, $path) {         $url = $this->url . '/tracks';         $data = array(             'oauth_token' => $this->accesstoken,             'track[title]' => $title,             'track[asset_data]' => new curlfile(realpath($path), 'audio/mpeg')         );          $result = $this->request($url, $data, 'post');         return $result;     }      private function request($url, $data, $method) {         $curl = curl_init();         if ($method === 'post') {             curl_setopt($curl, curlopt_post, true);             curl_setopt($curl, curlopt_postfields, $data);         } else {             $url .= '?' . http_build_query($data);         }         curl_setopt($curl, curlopt_url, $url);         curl_setopt($curl, curlopt_ssl_verifypeer, false);         curl_setopt($curl, curlopt_returntransfer, true);         curl_setopt($curl, curlopt_followlocation, true);         $result = json_decode(curl_exec($curl));         curl_close($curl);          return $result;     } }  $soundcloud = new soundcloudapi(api_url, client_id, client_secret); $soundcloud->auth(email, password); $result = $soundcloud->upload('my test file', file);  echo '<pre>'; print_r($result); echo '</pre>'; 

api response looks like:

stdclass object (     [kind] => track     [id] => 273327360     [created_at] => 2016/07/12 15:25:38 +0000     [user_id] => 117636904     [duration] => 0     [commentable] => 1     [state] => processing     [original_content_size] =>      [last_modified] => 2016/07/12 15:25:38 +0000     [sharing] => public     [tag_list] =>      [permalink] => my-test-file-2     [streamable] => 1     [embeddable_by] =>     [downloadable] =>      [purchase_url] =>      [label_id] =>      [purchase_title] =>      [genre] =>      [title] => test file     [description] =>      [label_name] =>      [release] =>      [track_type] =>      [key_signature] =>      [isrc] =>      [video_url] =>      [bpm] =>      [release_year] =>      [release_month] =>      [release_day] =>      [original_format] => unknown     [license] => all-rights-reserved     [uri] => https://api.soundcloud.com/tracks/273327360     [user] => stdclass object         (             [id] => 117636904             [kind] => user             [permalink] => kostya-shkryob             [username] => kostya shkryob             [last_modified] => 2016/07/12 14:51:09 +0000             [uri] => https://api.soundcloud.com/users/117636904             [permalink_url] => http://soundcloud.com/kostya-shkryob             [avatar_url] => https://i1.sndcdn.com/avatars-000108579156-l8ndy9-large.jpg         )      [user_playback_count] => 1     [user_favorite] =>      [permalink_url] => http://soundcloud.com/kostya-shkryob/my-test-file-2     [artwork_url] =>      [waveform_url] => https://a1.sndcdn.com/images/player-waveform-medium.png     [stream_url] => https://api.soundcloud.com/tracks/273327360/stream     [download_url] => https://api.soundcloud.com/tracks/273327360/download     [playback_count] => 0     [download_count] => 0     [favoritings_count] => 0     [comment_count] => 0     [attachments_uri] => https://api.soundcloud.com/tracks/273327360/attachments     [secret_token] => s-dneij     [secret_uri] => https://api.soundcloud.com/tracks/273327360?secret_token=s-dneij     [downloads_remaining] => 100 ) 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -