2014-09-29 67 views
0

我有一個包含soundcloud曲目URL列表的文本文件。我也有一個php文件,可以隨機選擇這些URL中的一個,並允許我在不同的php文件中接收它。現在由於某種原因,我將$ url設置爲隨機選擇的URL並在下一行中調用它。我得到的僅僅是URL的文本版本,而不是iframe soundcloud嵌入代碼。SoundCloud動態URL問題

<?php 

    //Get the SoundCloud URL 
    $url = $_GET['type'] = 0; include 'http://example.com/randomselector.php'; 

    //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; 
?> 

我試圖在第二行手動插入一個URL,並且工作正常。這意味着我得到了iframe嵌入代碼。

這是randomselector.php

<?php 

/* File, where the random text/quotes are stored one per line */ 
$settings['text_from_file'] = 'http://example.com/list.txt'; 

/* 
    How to display the text? 
    0 = raw mode: print the text as it is, when using RanTex as an include 
    1 = Javascript mode: when using Javascript to display the quote 
*/ 
$settings['display_type'] = 0; 

/* Allow on-the-fly settings override? 0 = NO, 1 = YES */ 
$settings['allow_otf'] = 1; 

/******************************************************************************* 
*******************************************************************************/ 

/* Override type? */ 
if ($settings['allow_otf'] && isset($_GET['type'])) 
{ 
    $type = intval($_GET['type']); 
} 
else 
{ 
    $type = $settings['display_type']; 
} 

/* Get a list of all text options */ 
if ($settings['text_from_file']) 
{ 
    $settings['quotes'] = file($settings['text_from_file']); 
} 

/* If we have any text choose a random one, otherwise show 'No text to choose from' */ 
if (count($settings['quotes'])) 
{ 
    $txt = $settings['quotes'][array_rand($settings['quotes'])]; 
} 
else 
{ 
    $txr = 'No text to choose from'; 
} 

/* Output the image according to the selected type */ 
if ($type) 
{ 
    /* New lines will break Javascript, remove any and replace them with <br /> */ 
    $txt = nl2br(trim($txt)); 
    $txt = str_replace(array("\n","\r"),'',$txt); 
    echo 'document.write(\''.addslashes($txt).'\')'; 
} 
else 
{ 
    echo $txt; 
} 
?> 

這是LIST.TXT

Link1 
Link2 
Link3 
+0

爲了幫助你,請分享randomselector.php /列表文件。你是完全正確的,我已經在file_get_contents - $ url = link之前添加了一行。它將按預期工作。 http://lab.sourcloud.com/stackoverflow/26092137/ – devbnz 2014-09-29 10:58:00

+0

我添加了randomselector.php和列表到我原來的問題。 – pyalb 2014-09-29 16:56:48

回答

0

嘗試捲曲insted的的file_get_contents的。

https://developers.soundcloud.com/docs/api/reference#oembed

見變化:

<?php 

    //Get the SoundCloud URL 
    $url = $_GET['type'] = 0; 
    include 'randomselector.php'; 

    $url = getUrl(); 

    $curlurl = 'http://soundcloud.com/oembed?format=json&url='.$url; 

    //curl 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_URL,$curlurl); 
    $result=curl_exec($ch); 
    curl_close($ch); 

    //sometimes you will get an error 
// $result = file_get_contents($curlurl); 

    $result = json_decode($result, true); 
    echo $result['html']; 
?> 

randomselector.php

<?php 
function getUrl(){ 
/* File, where the random text/quotes are stored one per line */ 
$settings['text_from_file'] = 'list.txt'; 

/* 
    How to display the text? 
    0 = raw mode: print the text as it is, when using RanTex as an include 
    1 = Javascript mode: when using Javascript to display the quote 
*/ 
$settings['display_type'] = 0; 

/* Allow on-the-fly settings override? 0 = NO, 1 = YES */ 
$settings['allow_otf'] = 1; 

/******************************************************************************* 
*******************************************************************************/ 

/* Override type? */ 
if ($settings['allow_otf'] && isset($_GET['type'])) 
{ 
    $type = intval($_GET['type']); 
} 
else 
{ 
    $type = $settings['display_type']; 
} 

/* Get a list of all text options */ 
if ($settings['text_from_file']) 
{ 
    $settings['quotes'] = file($settings['text_from_file']); 
} 

/* If we have any text choose a random one, otherwise show 'No text to choose from' */ 
if (count($settings['quotes'])) 
{ 
    $txt = $settings['quotes'][array_rand($settings['quotes'])]; 
} 
else 
{ 
    $txr = 'No text to choose from'; 
} 

/* Output the image according to the selected type */ 
if ($type) 
{ 
    /* New lines will break Javascript, remove any and replace them with <br /> */ 
    $txt = nl2br(trim($txt)); 
    $txt = str_replace(array("\n","\r"),'',$txt); 
// echo 'document.write(\''.addslashes($txt).'\')'; 
} 
else 
{ 
// echo $txt; 
} 
return $txt; 
} 
?> 
+0

這可行,但我如何將auto_play和show_comments添加到此iframe?我做了這樣的事情: '$ curlurl ='http://soundcloud.com/oembed?format = json&url ='。$ url。'&auto_play = true&show_comments = false'' 但是對於某些原因,它隻影響其中一個網址從list.txt – pyalb 2014-09-30 23:53:22

+0

不知道你的意思是隻影響其中一個網址。 http://soundcloud.com/oembed?format=json&auto_play=true&show_comments=false&url=對我來說工作正常。在這裏檢查。 http://lab.sourcloud.com/stackoverflow/26092137/ – devbnz 2014-10-01 13:19:56