PHP exec 从 XML 数据加载并执行单个 URL
PHP exec load and execute individual URL from XML data
我在尝试对从 XML 加载的每个 URL 执行命令时遇到问题,每次我按下按钮时,都会加载相同的 URL。
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 &");
}
?>
你做错的是你制作了 3 个没有 ID 的表单,它让每个提交按钮都将所有 URL 发送到后端。将增量 ID 放入表单中。下面的代码有效。
<?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 &" );
}
?>
我在尝试对从 XML 加载的每个 URL 执行命令时遇到问题,每次我按下按钮时,都会加载相同的 URL。
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 &");
}
?>
你做错的是你制作了 3 个没有 ID 的表单,它让每个提交按钮都将所有 URL 发送到后端。将增量 ID 放入表单中。下面的代码有效。
<?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 &" );
}
?>