php如何实现播放列表,使用PHP在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);

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值