使用 PHP 在 SoundCloud 中创建播放列表

Posted

技术标签:

【中文标题】使用 PHP 在 SoundCloud 中创建播放列表【英文标题】:Create playlist in SoundCloud with PHP 【发布时间】:2010-09-09 05:47:36 【问题描述】:

我正在创建一个集成了 SoundCloud API 的音乐网站。

现在我想在运行时创建一个播放列表并将曲目附加到该播放列表。

我的代码如下:

class Soundcloud 

    const VERSION = '1.1';

    public static $urls = array(
        'api' => 'http://api.soundcloud.com/',
        'oauth' => array(
            'access' => 'http://api.soundcloud.com/oauth/access_token',
            'authorize' => 'http://soundcloud.com/oauth/authorize',
            'request' => 'http://api.soundcloud.com/oauth/request_token'
        )
    );

    function __construct($consumer_key, $consumer_secret, $oauth_token = null, $oauth_token_secret = null) 
        $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
        $this->consumer = new OAuthConsumer($consumer_key, $consumer_secret);

        if (!empty($oauth_token) && !empty($oauth_token_secret)) 
            $this->token = new OAuthConsumer($oauth_token, $oauth_token_secret);
         else 
            $this->token = null;
        
    

    function get_authorize_url($token) 
        if (is_array($token)) 
            $token = $token['oauth_token'];
        


        return $this->_get_url('authorize') . '?oauth_token=' . $token;
    

    function get_request_token($oauth_callback) 
        $request = $this->request(
            $this->_get_url('request'),
            'POST',
            array('oauth_callback' => $oauth_callback)
        );
        $token = $this->_parse_response($request);

        $this->token = new OAuthConsumer(
            $token['oauth_token'],
            $token['oauth_token_secret']
        );

        return $token;
    

    function get_access_token($token) 
        $response = $this->request(
            $this->_get_url('access'),
            'POST',
            array('oauth_verifier' => $token)
        );
        $token = $this->_parse_response($response);
        $this->token = new OAuthConsumer(
            $token['oauth_token'],
            $token['oauth_token_secret']
        );

        return $token;
    

    function upload_track($post_data, $asset_data_mime = null, $artwork_data_mime = null) 
        $body = '';
        $boundary = '---------------------------' . md5(rand());
        $crlf = "\r\n";
        $headers = array(
            'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
            'Content-Length' => 0
        );

        foreach ($post_data as $key => $val) 
            if (in_array($key, array('track[asset_data]', 'track[artwork_data]'))) 
                $body .= '--' . $boundary . $crlf;
                $body .= 'Content-Disposition: form-data; name="' . $key . '"; filename="' . basename($val) . '"' . $crlf;
                $body .= 'Content-Type: ' . (($key == 'track[asset_data]') ? $asset_data_mime : $artwork_data_mime) . $crlf;
                $body .= $crlf;
                $body .= file_get_contents($val) . $crlf;
             else 
                $body .= '--' . $boundary . $crlf;
                $body .= 'Content-Disposition: form-data; name="' . $key .'"' . $crlf;
                $body .= $crlf;
                $body .= $val . $crlf;
            
        

        $body .= '--' . $boundary . '--' . $crlf;
        $headers['Content-Length'] += strlen($body);

        return $this->request('tracks', 'POST', $body, $headers);
    

    function request($resource, $method = 'GET', $args = array(), $headers = null) 

        if (!preg_match('/http:\/\//', $resource)) 
            $url = self::$urls['api'] . $resource;
         else 
            $url = $resource;
        

        if (stristr($headers['Content-Type'], 'multipart/form-data')) 
            $body = false;
         elseif (stristr($headers['Content-Type'], 'application/xml')) 
            $body = false;
         else 
            $body = true;
        

        $request = OAuthRequest::from_consumer_and_token(
            $this->consumer,
            $this->token,
            $method,
            $url,
            ($body === true) ? $args : null
        );
        $request->sign_request($this->sha1_method, $this->consumer, $this->token);

        return $this->_curl(
            $request->get_normalized_http_url(),
            $request,
            $args,
            $headers
        );
    

    private function _build_header($headers) 
        $h = array();

        if (count($headers) > 0) 
            foreach ($headers as $key => $val) 
                $h[] = $key . ': ' . $val;
            

            return $h;
         else 
            return $headers;
        
    

    private function _curl($url, $request, $post_data = null, $headers = null) 
        $ch = curl_init();
        $mime = (stristr($headers['Content-Type'], 'multipart/form-data')) ? true : false;
        $headers['User-Agent'] = (isset($headers['User-Agent']))
            ? $headers['User-Agent']
            : 'php SoundCloud/' . self::VERSION;
        $headers['Content-Length'] = (isset($headers['Content-Length']))
            ? $headers['Content-Length']
            : 0;
        $headers = (is_array($headers)) ? $this->_build_header($headers) : array();
        $options = array(
            CURLOPT_URL => $url,
            CURLOPT_HEADER => false,
            CURLOPT_RETURNTRANSFER => true
        );

        if (in_array($request->get_normalized_http_method(), array('DELETE', 'PUT'))) 
            $options[CURLOPT_CUSTOMREQUEST] = $request->get_normalized_http_method();
            $options[CURLOPT_POSTFIELDS] = '';
        

        if (is_array($post_data) && count($post_data) > 0 || $mime === true) 
            $options[CURLOPT_POSTFIELDS] = (is_array($post_data) && count($post_data) == 1)
                ? ((isset($post_data[key($post_data)])) ? $post_data[key($post_data)] : $post_data)
                : $post_data;
            $options[CURLOPT_POST] = true;
        

        $headers[] = $request->to_header();
        $options[CURLOPT_HTTPHEADER] = $headers;

        curl_setopt_array($ch, $options);

        $response = curl_exec($ch);
        $this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close($ch);

        return $response;
    

    private function _get_url($type) 
        return (array_key_exists($type, self::$urls['oauth']))
            ? self::$urls['oauth'][$type]
            : false;
    

    private function _parse_response($response) 
        parse_str($response, $output);

        return (count($output) > 0) ? $output : false;
    


 $soundcloud = new Soundcloud(
        $consumer_key,
        $consumer_secret,
        $_SESSION['oauth_access_token'],
        $_SESSION['oauth_access_token_secret']
    );

$post_data1 = array('playlist[title]'=>'This is my set' );
 $headers = array('Content-Type' => 'application/xml');
 $soundcloud->request('playlists', 'POST', $post_data1,$headers);

但它没有创建任何播放列表。

【问题讨论】:

你读过 github 上的文档吗? wiki.github.com/soundcloud/api 是的,尝试过但没有成功 你从服务器得到什么响应(代码、消息)?你有什么例外吗? 【参考方案1】:

你的类(Soundcloud 类)不应该扩展一些东西吗?

【讨论】:

【参考方案2】:

您的班级不应该实际生成播放列表文件吗?您听说过 XSPF 吗?

【讨论】:

以上是关于使用 PHP 在 SoundCloud 中创建播放列表的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 javascript 创建 Soundcloud 播放列表 ( set )?

Soundcloud iOS API - 从链接播放声音

如何播放 SoundCloud RTMP 流?

使用 SoundCloud 默认音频小部件,您可以将其设置为从 JavaScript 随机播放吗?

使用 Go 从 SoundCloud MP3 流媒体 URL 播放?

播放soundcloud音频时暂停youtube视频