使用PHP在SoundCloud上上传轨道

时间:2016-07-12 13:14:14

标签: php api soundcloud

我正在尝试使用此库上传曲目。

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

其他服务,例如身份验证,获取访问令牌,创建播放列表都运行良好,但是当我尝试上传跟踪时,它失败了,并在我尝试的几种情况下返回代码[0,422,500]。

案例1:失败

$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")));

案例2:失败

$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")));

案例3:失败

$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")));

案例4:失败

$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")));

它显示了弃用方法' @'使用文件,所以我使用CURLFile类方法来处理curl文件。

如果我身边的错误,请告诉我。请注意,我将访问令牌设置为请求标头,因此授权时不会出错。将数据发送到SoundCloud可能存在愚蠢的错误。

1 个答案:

答案 0 :(得分:2)

您可以直接拨打Soundcloud HTTP API而无需包装。请参阅下面的代码示例:

<?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响应如下:

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
)