【问题标题】:How to get Soundcloud embed code by soundcloud.com url如何通过 soundcloud.com url 获取 Soundcloud 嵌入代码
【发布时间】:2014-01-01 15:44:38
【问题描述】:

我已经研究了几个小时,但没有成功。基本上我有一个 cms,用户可以在其中放置 soundcloud url,如“https://soundcloud.com/cade-turner/cade-turner-symphony-of-light”,然后页面可以显示嵌入的 iframe。我把 api 文档弄红了一段时间,但找不到任何相关的东西。这篇帖子here 已经谈过了,但我只是不太明白答案,我检查了oEmbedoEmbed reference,但找不到合适的例子。谁有更多提示?

编辑: 感谢Jacob的回答,我终于设法通过使用ajax来做到这一点。

var trackUrl = 'THE_URL';
var Client_ID = 'CLIENT_ID';//you have to register in soundcound developer first in order to get this id 
$.get(//make an ajax request
    'http://api.soundcloud.com/resolve.json?url=' + trackUrl + '&client_id=' + Client_ID, 
    function (result) {//returns json, we only need id in this case
        $(".videowrapper, .exhibitions-image, iframe").replaceWith('<iframe width="100%" height="100%" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/' + result.id +'&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true"></iframe>');//the iframe is copied from soundcloud embed codes
    }
);

【问题讨论】:

  • 很好的答案!您将如何为曲目或专辑执行此操作?
  • @Operator “为曲目或专辑”是什么意思?我正在做一个轨道。 ajax 是为了得到这个轨道的 id

标签: javascript php soundcloud


【解决方案1】:

我在我的 coden-ps 中找到了这个完全符合您的目的,即使无需注册客户端 ID。

//Get the SoundCloud URL
$url="https://soundcloud.com/epitaph-records/this-wild-life-history";
//Get the JSON data of song details with embed code from SoundCloud oEmbed
$getValues=file_get_contents('http://soundcloud.com/oembed?format=js&url='.$url.'&iframe=true');
//Clean the Json to decode
$decodeiFrame=substr($getValues, 1, -2);
//json decode to convert it as an array
$jsonObj = json_decode($decodeiFrame);

//Change the height of the embed player if you want else uncomment below line
// echo $jsonObj->html;
//Print the embed player to the page
echo str_replace('height="400"', 'height="140"', $jsonObj->html);

【讨论】:

  • 好方法!谢谢你。音乐品味也很好:-)
  • 这是一个比例外答案更好的解决方案。并感谢@Greg 提供文档链接。
  • 无法获得 SoundCloud API 密钥,所以这很有帮助!
  • $html = $jsonObj-&gt;html; $html = str_replace('visual=true', 'visual=false', $html); $html = str_replace('height="400"', 'height="140"', $html);
【解决方案2】:

对于你选择的曲目,嵌入代码如下:

<iframe width="100%" height="166" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/101276036&amp;color=ff6600&amp;auto_play=false&amp;show_artwork=true"></iframe>

唯一的独特之处是 Track ID,在本例中为 101276036

因此,当您只有 URL 时,您真正的问题是试图找到 Track ID,而 Soundcloud API 提供了一个名为 resolve 的方法来做到这一点。

require_once 'Services/Soundcloud.php';
$client = new Services_Soundcloud('YOUR_CLIENT_ID');
$track_url = 'https://soundcloud.com/cade-turner/cade-turner-symphony-of-light'; // Track URL
$track = json_decode($client->get('resolve', array('url' => $track_url)));
$track->id; // 101276036 (the Track ID)

然后您可以存储此 ID,或生成并存储您想要显示的 HTML。

【讨论】:

【解决方案3】:

我正在寻找类似的东西,发现这真的很有帮助:

https://developers.soundcloud.com/docs/oembed#introduction

试试这个 CURL 命令:

curl "http://soundcloud.com/oembed" -d 'format=json' -d 'url=http://soundcloud.com/forss/flickermood'

或者这个 jQuery ajax 请求:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://soundcloud.com/oembed",
  "method": "POST",
  "headers": {},
  "data": {
    "format": "json",
    "url": "http://soundcloud.com/forss/flickermood"
  }
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

【讨论】:

    【解决方案4】:

    我遇到了与上述相同的问题。我有不再工作的旧嵌入代码,不幸的是,我仅限于 HTML。看到上面的答案对我不起作用。所以我决定,我会尝试一些东西,看看它是否有效,哦,我的,它确实有效!您不再需要实际转换内容。您可以只使用旧网址。这是我的html代码:

    <iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay"
    src="https://w.soundcloud.com/player/?url=https%3A//soundcloud.com/LPChip/internal-meganism
    &color=%23ff5500&auto_play=false&hide_related=false
    &show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true">
    </iframe>
    

    我在上面添加了 enter 以使其可读,但应该是一行代码。

    原因是我在论坛上使用自定义BBCode模式运行它,我想支持它如下:[soundcloud=Artist]song[/soundcloud]

    在上述情况下,这是[soundcloud=LPChip]Internal-meganism[/soundcloud]

    BBCode的实际html如下:

    <iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//soundcloud.com/{option1}/{content}&color=%23ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false&show_teaser=true&visual=true"></iframe>
    

    这适用于SMF 2.0.15custom BBCode mod,设置为未解析的相等内容。

    演示:https://nifflas.lp1.nl/index.php?topic=6630.0

    【讨论】:

      【解决方案5】:

      这是我编写的基于 jQuery 的 Javascript 解决方案。

      它不需要客户 ID。

      确保包含 jQuery,并将其添加到文档的 &lt;head&gt;

      <script type="text/javascript">
      document.addEventListener("DOMContentLoaded", function(event) {
          (function ($) {
              $.fn.scembed = function(){
              var datasource  = 'http://soundcloud.com/oembed';
              return this.each(function () {
                  var container = $(this);
                  var mediasource = $(container).attr("sc_url");
                  var params = 'url=' + mediasource + '&format=json&iframe=true&maxwidth=480&maxheight=120&auto_play=false&show_comments=false';
                  $.ajaxopts = $.extend($.ajaxopts, {
                                      url: datasource,
                                      data: params,
                                      dataType: 'json',
                                      success: function (data, status, raw) {
                                          $(container).html(data.html);
                                      },
                                      error: function (data, e1, e2) {
                                          $(container).html("Can't retrieve player for " + mediasource);
                                      },
                                  });
                  $.ajax($.ajaxopts);
                  });
              };
          })(jQuery);
      
          $(function(){
              $("div.sc-embed").scembed();
          });
      });
      </script>
      

      要将 Soundcloud 播放器插入页面,请创建一个 &lt;div&gt;,为其指定一个 sc-embed 类,并为其指定一个名为 sc_url 的属性,该属性应设置为 Soundcloud 页面的 URL。

      例如:

      <div class="sc-embed" sc_url="http://soundcloud.com/forss/flickermood"></div>
      

      要插入多个播放器,请使用多个 &lt;div&gt;s,并为 sc_url 提供不同的值。

      您可以更改 Javascript 中的 params 变量以启用或禁用此处列出的播放器选项:https://developers.soundcloud.com/docs/oembed#introduction

      【讨论】:

        【解决方案6】:

        我不确定之前是否有人尝试过此操作,但目前(2021 年 2 月)它正在将基本的 soundcloud 链接传递到 iframe 链接的 ?url 参数中。 “官方”嵌入代码使用另一个 ID,但常规链接也可以正常工作(目前)。

        例如,这个指向 soundcloud 的链接可以嵌入为https://soundcloud.com/anton-jarvis-206182017/loves-philosophy-by-percy-bysshe-shelley

        &lt;iframe width="100%" height="300" scrolling="no" frameborder="no" allow="autoplay" src="https://w.soundcloud.com/player/?url=https%3A//soundcloud.com/anton-jarvis-206182017/loves-philosophy-by-percy-bysshe-shelley&amp;color=%23ff5500&amp;auto_play=false&amp;hide_related=false&amp;show_comments=true&amp;show_user=true&amp;show_reposts=false&amp;show_teaser=true&amp;visual=true"&gt;&lt;/iframe&gt;

        感谢@lpchip 的回答,他的回答启发了我尝试这个

        【讨论】:

          【解决方案7】:

          我用 javascript 做到了这一点,没有使用 jQuery:

          var formData  = new FormData();
          
          formData.append("format", "json");
          formData.append("url", "http://soundcloud.com/forss/flickermood");
          
          fetch('http://soundcloud.com/oembed', {
              method: 'POST',
              body: formData
          }).then(function (response) {
              console.log(response);
          });
          

          【讨论】:

            【解决方案8】:

            这是一个带有 CURL 的 PHP 示例

            // Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
            $ch = curl_init();
            
            curl_setopt($ch, CURLOPT_URL, 'http://soundcloud.com/oembed');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, "format=json&url=http://soundcloud.com/forss/flickermood");
            
            $headers = array();
            $headers[] = 'Content-Type: application/x-www-form-urlencoded';
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            
            $result = curl_exec($ch);
            if (curl_errno($ch)) {
                echo 'Error:' . curl_error($ch);
            }
            curl_close($ch);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-12-10
              • 2016-08-24
              • 1970-01-01
              相关资源
              最近更新 更多