Search code examples
phphtmlshellshell-exec

PHP exec load and execute individual URL from XML data


I'm facing a problem trying to execute a command with each individual URL loaded from XML, everytime I press the button the same URL is being loaded.

XML

<?xml version="1.0" encoding="utf-8"?>
<data>
  <channels>
    <banner>https://yt3.ggpht.com/ytc/AAUvwnhiSj68Il28UVufce2uB4FGuMFMjsgdnZWtEES1N1c=s88-c-k-c0xffffffff-no-rj-mo</banner>
    <url>https://www.youtube.com/channel/UCoMdktPbSTixAyNGwb-UYkQ/live</url>
  </channels>
  <channels>
    <banner>https://yt3.ggpht.com/ytc/AAUvwnjes9QiTK0Sv-6jI-TW-pjwqG4HhexOh2R3br0lrus=s88-c-k-c0xffffffff-no-rj-mo</banner>
    <url>https://www.youtube.com/channel/UCeY0bbntWzzVIaj2z3QigXg/live</url>
  </channels>
  <channels>
    <banner>https://yt3.ggpht.com/ytc/AAUvwnjQUqXvG-C_hEeRySPaaGMS1REtGd05a9RSLwIj7xc=s88-c-k-c0xffffffff-no-rj-mo</banner>
    <url>https://www.youtube.com/channel/UCNye-wNBqNL5ZzHSJj3l8Bg/live</url>
  </channels>
</data>

PHP

<?php
echo"<div class='container'>";
$xml = simplexml_load_file('channels.xml') or die('Failed to read data');
foreach ( $xml->channels as $channelsElement ) {
echo "<img class='responsive' src='";
echo $channelsElement->banner;
echo "'><br>";
echo "<form method='post' action='index.php'>";
echo "<input type='hidden' type='text' name='url' value='";
echo $channelsElement->url;
echo "'><br>";
echo "<input id='wrapper' type='submit' name='submit' value='PLAY CHANNEL'>";
echo "<br>";
echo"</div>";
}
?>
<?php
if(isset($_POST['url'])) {
    shell_exec("sudo killall vlc");
    shell_exec("sudo killall omxplayer.bin");
    shell_exec("sudo omxplayer `youtube-dl -g ".($_POST['url'])." ` >> /dev/null &");
}
?>

Solution

  • What you did wrong is you are making 3 forms without id and it makes every submit button send all the URLs to the backend. Put incremental id to form.. below code works.

    
    <?php ?>
        <div class='container'>
    
            <?php
            $xml = simplexml_load_file( 'channels.xml' ) or die( 'Failed to read data' );
            $i = 0;
    
    
            foreach ( $xml->channels as $channelsElement ):
                ?>
    
                <img class='responsive' src='<?= $channelsElement->banner ?>'><br>
                <form method='post' action='index.php' id=form-<?= $i ?> >
                    <input type='hidden' type='text' name='url' value='<?= $channelsElement->url ?>'><br>
                    <input id='wrapper' type='submit' name='submit' value='PLAY CHANNEL'>
                </form>
                <br>
    
                <?php
                $i ++;
            endforeach;
            ?>
        </div>
    
    <?php
    if ( isset( $_POST['url'] ) ) {
        echo $_POST['url'];
        shell_exec( "sudo killall vlc" );
        shell_exec( "sudo killall omxplayer.bin" );
        shell_exec( "sudo omxplayer `youtube-dl -g " . ( $_POST['url'] ) . " ` >> /dev/null &" );
    }
    ?>