Rendering SoundCloud widget for a private track using PHP API

北城以北 提交于 2019-12-08 03:18:46

问题


I am trying to render a SoundCloud HTML5 widget using the PHP API, but every time I run the command I think should return the HTML for the widget, I simply get an Exception:

The requested URL responded with HTTP code 302

I realise this is a redirect. What I don't know is why that's all I ever get, or what to do about it to actually get the widget HTML.

The documentation on the API says that to embed the widget using PHP you should do this:

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

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

    // get a tracks oembed data
    $track_url = 'http://soundcloud.com/forss/flickermood';
    $embed_info = $client->get('/oembed', array('url' => $track_url));

    // render the html for the player widget
    print $embed_info['html'];

I'm running this:

// NB: Fully authorised SoundCloud API instance all working prior to this line
// $this->api refers to an authorised instance of Services_Soundcloud

try {   
        $widget = array_pop(
            json_decode( $this->api->get('oembed', array('url' => $track_url)) )
        );

        print_r($widget);

    } catch (Exception $e)
    {
        print_r($e->getMessage());
    }

where "track_url" is actually the URL I get back when asking SoundCloud for a track object earlier in the app using the same API.

I'm not actually sure this URL is correct in the first place, because the track object I get back gives the 'uri' in the form:

[uri] => https://api.soundcloud.com/tracks/62556508

The documentation examples all have a straight http://soundcloud.com/username/track-permalink URL - but even using a known path to a public track the attempt to run the API oembed method fails... I still get a 302 Exception.

Finally, there are mentions of setting "allow_redirects" to false in the 'get' command, but this has no effect when I add to the parameters used to build the query to the API. I also tried adding additional cURL options, but that too had no effect.

I have definitely enabled API access to the track within SoundCloud.

Kind of banging my head off the wall on this. If anyone has any pointers I'd be very grateful to hear them. Just for clarity's sake, I am able to access all the user data, comments etc. via the API instance I have created, so it appears to be working fine.


回答1:


Thanks for pointing this out. There was a bug in the documentation that lead you astray. Sorry about that. I've updated the docs to fix the bug. Here's the updated code sample:

<?php

require_once 'Services/Soundcloud.php';

// create a client object with your app credentials
$client = new Services_Soundcloud('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET');
$client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1));

// get a tracks oembed data
$track_url = 'http://soundcloud.com/forss/flickermood';
$embed_info = json_decode($client->get('oembed', array('url' => $track_url)));

// render the html for the player widget
print $embed_info->html;

Note the differences:

  • You need to set CURLOPT_FOLLOWLOCATION to 1 as mentioned in the comments above.
  • You need to wrap the return from $client->get in json_decode
  • The result is an stdClass object, not an Array and so the html property has to be accessed using the -> operator.

Hope that helps. Feel free to comment in case you're still having problems and I'll amend my answer.



来源:https://stackoverflow.com/questions/12779601/rendering-soundcloud-widget-for-a-private-track-using-php-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!