Hitesh Dobariya
Hitesh Dobariya

Reputation: 11

Upload track on SoundCloud using PHP

I am trying to upload track by using this library.

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

Other services like authentication, getting access token, creating playlist are working fine but when I am trying to upload track it's failed and return code [0,422,500] in several cases in which I 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 to use '@' with file so I used CURLFile class method to handle file with curl.

Please Let me know if what is mistake from my side. Note that I set access token to request header so there is no error with authorization. there may be silly mistake with data to send to SoundCloud.

Upvotes: 1

Views: 1641

Answers (1)

Kostya Shkryob
Kostya Shkryob

Reputation: 2369

You can call Soundcloud HTTP API directly without wrapper. Please see my code sample below:

<?php
//App credentials. Create one at 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 to file to 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] => all
    [downloadable] => 
    [purchase_url] => 
    [label_id] => 
    [purchase_title] => 
    [genre] => 
    [title] => My 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
)

Upvotes: 2

Related Questions