使用 PHP 在 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可能会有愚蠢的错误。

您可以直接调用 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
)

相关内容

  • 没有找到相关文章

最新更新