使用 PHP API 为私人曲目渲染 SoundCloud 小部件 [英] Rendering SoundCloud widget for a private track using PHP API

查看:24
本文介绍了使用 PHP API 为私人曲目渲染 SoundCloud 小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHP API 呈现 SoundCloud HTML5 小部件,但是每次我运行命令时我认为应该返回小部件的 HTML,我只是得到一个异常:

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

我意识到这是一个重定向.我不知道为什么我只能得到这些,或者如何处理它才能真正获得小部件 HTML.

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.

API 上的文档说要使用 PHP 嵌入小部件,您应该这样做:

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'];

我正在运行这个:

// 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());
    }

其中track_url"实际上是我在之前使用相同 API 在应用程序中向 SoundCloud 询问轨道对象时返回的 URL.

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.

我一开始并不确定这个 URL 是否正确,因为我返回的 track 对象以以下形式提供了 'uri':

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

文档示例都有一个直接的 http://soundcloud.com/username/track-permalink URL - 但即使使用公共轨道的已知路径,尝试运行 API oembed 方法也会失败......我仍然收到 302 异常.

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.

最后,在get"命令中提到将allow_redirects"设置为 false,但是当我添加到用于构建 API 查询的参数时,这不起作用.我也尝试添加额外的 cURL 选项,但这也没有效果.

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.

我确实在 SoundCloud 中启用了对曲目的 API 访问.

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

有点把我的头撞到墙上.如果有人有任何指示,我将非常感激听到他们.为了清楚起见,我可以通过我创建的 API 实例访问所有用户数据、评论等,因此它似乎工作正常.

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.

推荐答案

感谢您指出这一点.文档中有一个错误导致您误入歧途.对于那个很抱歉.我已经更新了文档以修复错误.这是更新后的代码示例:

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;

注意区别:

  • 您需要按照上述评论中的说明将 CURLOPT_FOLLOWLOCATION 设置为 1.
  • 您需要将 $client->get 的返回值包装在 json_decode
  • 结果是一个 stdClass 对象,而不是 Array,因此必须使用 -> 访问 html 属性; 运算符.
  • 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.

这篇关于使用 PHP API 为私人曲目渲染 SoundCloud 小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆