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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址