Проблем с динамичния URL адрес на SoundCloud

Имам текстов файл, който съдържа списък с URL адреси на записи на soundcloud. Имам и php файл, който произволно избира един от тези URL адреси и ми позволява да го получа в различен php файл. Сега по някаква причина, когато задам $url като произволно избран URL адрес и го извикам на следващия ред. Това, което получавам, е само текстовата версия на URL адреса, а не кода за вграждане на iframe soundcloud.

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

person pyalb    schedule 29.09.2014    source източник
comment
За да ви помогнем, моля, споделете файла randomselector.php / list. Вие сте напълно прав, добавих ред преди file_get_contents - $url = връзка. Ще работи според очакванията. lab.sourcloud.com/stackoverflow/26092137   -  person hwsw    schedule 29.09.2014
comment
Добавих randomselector.php и списък към първоначалния си въпрос.   -  person pyalb    schedule 29.09.2014


Отговори (1)


Опитайте curl вместо 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;
}
?>
person hwsw    schedule 29.09.2014
comment
Това работи, но как да добавя auto_play и show_comments към този iframe? Направих нещо подобно: $curlurl = 'http://soundcloud.com/oembed?format=json&url='.$url.'&auto_play=true&show_comments=false' Но по някаква причина това засяга само един от URL адресите от list.txt - person pyalb; 01.10.2014
comment
Не знам какво имате предвид под засяга само един от URL адресите. soundcloud.com/ работи добре за мен. Проверете тук. lab.sourcloud.com/stackoverflow/26092137 - person hwsw; 01.10.2014