Error encountered while attempting to load JSON data into HTML audio element

I need to incorporate data from a JSON file into an HTML audio tag. While I've been able to extract the desired information from the JSON, I'm facing difficulty loading it into HTML. Here's what I've tried so far:

<?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: I now have this solution.

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

In addition, I aim to insert a variable in the second line following .

Can anyone guide me on accomplishing this task?

Answer №1

Follow the instructions below for proper execution:

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

View output here: http://prntscr.com/cl4ubs

Note:- The audio file is playing at my end and can be saved with the default name: http://prntscr.com/cl4vj8

Answer to your second question:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can the hreflang tag be used correctly in a React application that supports multiple languages?

I have a React application with 3 pages(routes) and I support 2 languages (English and Spanish). Should I insert the following code into the <head></head> section of the public\index.html file like this? <link rel="alternate" ...

PHP Ajax request to the same PHP script resulted in a response of null

As part of my development process, I am creating a single-page script called category.php to handle category management. This script includes an input button that triggers an AJAX call. <input type="button" id="btn" /> The jQuery code is u ...

Guide to Embedding an Image in a List in HTML

How do I resize an image according to the size specified in CSS, and ensure that it fits within a list of items where each item consists of both an image and a name? Currently, my code only displays the image in its original size. for(var i =0; i< o ...

In order to format my text, I must locate a specific character and encompass the text following it with HTML tags

When working with a list generated in Jekyll, I came across the need to emphasize certain words using strong tags. My solution was to incorporate a delimiter into the process. <li>100g of |sugar</li> This would transform into: <li>100g ...

A guide to extracting text from HTML elements with puppeteer

This particular query has most likely been asked numerous times, but despite my extensive search, none of the solutions have proven effective in my case. Here is the Div snippet I am currently dealing with: <div class="dataTables_info" id=&qu ...

What is the process for transforming the result of a .aspx file into a JSON format?

I am trying to convert the output of an .aspx page into a JSON object in order to accommodate a JSONP Ajax request. Here is what's happening on the page: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="mypage.aspx.cs" Inherits="folder_myp ...

Attempting to execute a PHP script through a JavaScript if statement

I have a form that requires validation with JavaScript. In one of the if statements, after all other conditions have been validated, I want to execute my PHP script that updates an SQL database with one of the passwords. Here is the final validation code: ...

Generating a generic list in C# using JArray

Currently, I am facing a challenge with my dynamic import process that involves converting Json into an object array. My main obstacle lies in creating a List<> based on a string reference to the class object. To elaborate, I am retrieving json data from ...

Using a JSON database to implement various user levels in a domain model

Here is the domain model that I believe to be accurate. I am currently attempting to implement this on a JSON database, but I am uncertain if I am headed in the right direction. From my understanding of JSON, there are no inherent relationships between obj ...

Using HTML and CSS to implement a broadened perspective for a specific design

https://i.stack.imgur.com/ckQHa.png Hello, I am facing an issue with a UX design that is optimized for 1200px resolution width. However, when the HTML is loaded in a browser on a larger window resolution, there is a 200px gap on the right side. How can I ...

Move a file from one directory to another directory that is two levels higher

Here is the script file path: C:\xampp\htdocs\wordpress\wp-content\plugins\test\test.php The goal is to move images from this location: C:\xampp\htdocs\wordpress\wp-content\uploads\2017&bs ...

Controlling the Flow of Events in JavaScript Documents

Explore the integration of two interconnected Javascript files and delve into managing event propagation between them effectively. 1st js file var red = [0, 100, 63]; var orange = [40, 100, 60]; var green = [75, 100, 40]; var blue = [196, 77, 55]; var ...

Stuck autoplay feature when clicking

There is an issue with the play function in the built-in browser audio player. Initially, it starts automatically with the following code... function play(){ var audio = document.getElementById("myaudio"); audio.play(); } However, when modifying the code ...

Having trouble with the JavaScript DOM rewrite for aligning text?

function modifyTextAlignment(){ document.getElementById("th1").style.textAlign = "right"; } #th1{ text-align:left; } <table> <tr><center><th colspan="2" id="th1">List of Important Emergency Contacts</th><center></tr& ...

Troubleshooting problems with displaying views due to asynchronous $http.get calls in AngularJS

Within my application, I am utilizing two separate API calls. The initial call retrieves a catalog of services along with their unique ID's. Subsequently, once the ID information is acquired, a second call is made to retrieve pricing data for each cor ...

All images must be arranged to fit seamlessly side by side regardless of the screen size

I'm currently experimenting with creating a website dedicated to my favorite TV shows. Upon entering the site, you are greeted with the main page that includes 7 images. Each image is linked to a specific webpage providing information about the corre ...

Collapse in Bootstrap without any Animation

How can I add animation to the Bootstrap Collapse feature on my website? I am using an icon from the Font Awesome library to trigger the collapse effect, but currently, it just pops up without any animation. What could be causing this issue? You can view ...

Accessing the phone number input from a form in Rails and verifying its presence in the database. Implementing distinct ajax responses based on the existence of the value

Looking for a way to verify phone numbers? The process involves users entering a phone number, submitting the form, and cross-referencing it with a database of phone number records to determine if it's valid. My goal is to provide different AJAX respo ...

Guide to creating a parallax scrolling effect with a background image

My frustration levels have hit an all-time high after spending days attempting to troubleshoot why parallax scrolling isn't functioning for a single image on a website I'm developing. To give you some context, here's the code I've been ...

Gaining entry to a JSON object within an array

I've completed various courses on JSON and put in a lot of effort to troubleshoot this bug. However, it seems that my question is too specific and I require a practical example. JSON { "date": "2017-10-15", "league": "NHL", "odds": "spreads", ...