Home » Php » php – How to include the JavaScript result into the player?

php – How to include the JavaScript result into the player?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I need to include the result of this JavaScript.

<script>
function file_get_contents(filename) {
fetch(filename).then((resp) => resp.text()).then(function(data) {
    url = JSON.parse(data)
    location.replace(url['stream]);
});
}
file_get_contents('http://api.stream.com/live');

(The result is something like: http://stream.com/live/livestream.m3u8)

here…

<script src="clappr.min.js"></script>
<script>var player = new Clappr.Player({source: "RESULT HERE"});</script>

How do I include the result? With document.write? Or should I use PHP too?

How to&Answers:

Can’t you just do this?

<script src="clappr.min.js"></script>
<script>
function file_get_contents(filename) {
    fetch(filename).then((resp) => resp.text()).then(function(data) {
        url = JSON.parse(data)
        location.replace(url['stream]);
    });
}
file_get_contents('http://api.stream.com/live');
var player = new Clappr.Player({source: file_get_contents('http://api.stream.com/live')});
</script>

Answer:

According to the documentation https://clappr.github.io/classes/Player.html, Clapper player is expecting a string value path that points to an MP4 file:

var player = new Clappr.Player({source: "http://your.video/here.mp4", parentId: "#player"});

What does file_get_contents return?

Have you tried getting the basics to work with a hardcoded MP4?

var player = new Clappr.Player({source: "http://api.stream.com/live/your.mp4", parentId: "#player"});