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

标签 php audio soundcloud

我正在创建一个集成了 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);

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

最佳答案

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

关于php - 使用 PHP 在 SoundCloud 中创建播放列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3674093/

相关文章:

php - 结果排序表

php - 在 Laravel 5.2 中插入数据后如何重定向到路由?

wordpress - 获取歌曲数据并将其放入共享按钮

javascript - 使用 Apple Remote 播放/暂停 Soundcloud 选项卡的脚本

jquery - Canvas 元素的 CSS(SoundCloud 持续时间)?

php - 在 PHP 中禁用回显和打印

php - MYSQLI Prepared Statement Insert and lookup value 来自其他表

android - 网站上的背景音频自动在笔记本电脑上运行,但在移动设备上不运行

javascript - 设置 Javascript 音频对象的 MIME 类型

Javascript如何使函数/变量全局可用