deimos krijgsman deimos krijgsman - 2 months ago 7
JSON Question

Parse error whan trying to load JSON into HTML audio tag

I want to use data from a JSON file in an HTML audio tag.
I am really close and able to get the data I want from the JSON but I can't load it into HTML. This is what I already tried

<?php
$json = file_get_contents('http://www.youtubeinmp3.com/fetch/format=JSON&video=http://www.youtube.com/watch?v=i62Zjga8JOM');
$data = json_decode($json);
$stream = $data->{'link'};
echo "<audio controls><source src= .$stream. "</audio>");
?>


Update: now I got this.

<?php
$json = file_get_contents('http://www.youtubeinmp3.com/fetch/?format=JSON&video=http://www.youtube.com/watch?v=1rLvsC6T4YI'); // this WILL do an http request for you
$data = json_decode($json);
$stream = $data->{'link'};
echo "<audio controls><source src= $stream. '</audio>')";
?>


I also want to add a variable to the second line after http://www.youtubeinmp3.com/fetch/format=JSON&video=http://www.youtube.com/watch?v=

How can I do this?

Answer

You need to do it like below:-

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$json = file_get_contents('http://www.youtubeinmp3.com/fetch/?format=JSON&video=http://www.youtube.com/watch?v=1rLvsC6T4YI');
$data = json_decode($json,true);
$stream = $data['link'];
?>
<!DOCTYPE html>
<html>
<body>

<audio controls>
  <source src="<?php echo $stream;?>" type="audio/mp3">
</audio>

</body>
</html>

Output:- http://prntscr.com/cl4ubs

Note:- it's running audio file at my end and i am able to save it also with the default name of:- http://prntscr.com/cl4vj8

Your second question answer:-

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$variable_to_add = '1rLvsC6T4YI';
$json = file_get_contents("http://www.youtubeinmp3.com/fetch/?format=JSON&video=http://www.youtube.com/watch?v=$variable_to_add");
$data = json_decode($json,true);
$stream = $data['link'];
?>
<!DOCTYPE html>
<html>
<body>

<audio controls>
  <source src="<?php echo $stream;?>" type="audio/mp3">
</audio>

</body>
</html>