du060334 2014-02-20 19:57
浏览 47
已采纳

Soundcloud在PHP上解析

I'm trying to setup a simple app that should:

  • get the client ID of an artist from its URL
  • display the two latest tracks

However I'm not that practical with soundcloud and i just know basic php. I started to play with soundcloud but i wasn't able to handle it. A problem i have is that any code i write, it gets

Fatal error: Uncaught exception 'Services_Soundcloud_Invalid_Http_Response_Code_Exception' with message 'The requested URL responded with HTTP code 302.'

The easiest setup come straight from the documentation and is an example to retrieve the comments from the track id, starting from a give URL.

<?php
require_once 'Services/Soundcloud.php';

// create a client object with your app credentials
$client = new Services_Soundcloud('my_client','my_secret');

// a permalink to a track
$track_url = 'https://url_to_a_track';

// resolve track URL into track resource
$track = json_decode($client->get('resolve', array('url' => $track_url), array('CURLOPT_FOLLOWLOCATION', TRUE )));

// now that we have the track id, we can get a list of comments, for example
foreach (json_decode($client->get('tracks/' . $track->id . 'comments')) as $c)
print 'Someone said: ' . $c->body . ' at ' . $c->timestamp . "
"; ?>

Just added ('CURLOPT_FOLLOWLOCATION', TRUE) because I've read about it around the web... And I always get the fatal error... why?

  • 写回答

3条回答 默认 最新

  • dongying6896 2014-02-21 08:27
    关注

    I've been able to solve it. If the resources are public, then you don't need to authenticate. Here's the code that:

    • Get user URL from a wordpress custom-field
    • Retrieve ID from json object
    • Retrieve the latest 2 tracks and display 2 embedded players

    It just need your own Client ID you can get easily by registering on soundcloud developers section, and then substitute it to {Your_ID}

        // get user URL from Wordpress custom field
        $sc_url = get_post_meta(get_the_id(), 'sc_url', true);
    
        // if $sc_url is not empty, do
        if (!empty($sc_url)) {
    
        $unparsed_json = file_get_contents('https://api.soundcloud.com/resolve.json?url='.$sc_url.'&client_id={Your_ID}');
    
        $json_object = json_decode($unparsed_json);
    
        // retrieve the user ID from json_object
        $roster_id = $json_object->{'id'};
    
        // get last two tracks from the user and generate embed code for each tracks 
        $tracks_json = file_get_contents('http://api.soundcloud.com/users/'.$roster_id.'/tracks?client_id={Your_ID}&order=latest&limit=2&format=json');
        $tracks = json_decode($tracks_json);
        foreach ($tracks as $track){
        $trackID = $track->id;
        echo '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F'.$trackID.'"></iframe>';
        }}
    

    Hope it can help others :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加