Php Curl解析数组上的m3u文件

时间:2018-04-29 04:31:47

标签: php parsing curl

希望你能帮助我。 我有abc.m3u文件,其中包含例如:

#EXTM3U

#EXTINF:-1,CH1

https://fakedummy.net/tre002/55D6D-channel7/1.m3u8

#EXTINF:-1,CH2

https://fakeone.net/teb002/12333-channel8/1.m3u8

#EXTINF:-1,CH3

http://fake.fake/tv/m3u8/fake.m3u8?id=fake_abc

#EXTINF:-1,CH4

http://test.test:13000/xxx/jgjkg/671.ts

有人可以告诉我如何在php数组中解析整个文件m3u(它是一个非常大的文件),以便以后能够在vlc,ffmpeg或以下格式的任何播放器上播放输出:

http://localhost/myphpfile.php?id=CH1 >to play first stream on abc.m3u file 
https://fakedummy.net/tre002/55D6D-channel7/1.m3u8

http://localhost/myphpfile.php?id=CH2 >to play 2nd stream on abc.m3u file
https://fakeone.net/teb002/12333-channel8/1.m3u8

http://localhost/myphpfile.php?id=CH3 >to play 3rd stream on abc.m3u file
http://fake.fake/tv/m3u8/fake.m3u8?id=fake_abc

http://localhost/myphpfile.php?id=CH4 >to play 4th stream on abc.m3u file
http://test.test:13000/xxx/jgjkg/671.ts

我已经尝试了以下代码,它仅适用于第一个chl

    <?php
    $handle = curl_init();
    $url = "abc.m3u";
    $domain = preg_replace("(^https?://)", "", $url );
    $header = array('Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3');
   curl_setopt($handle, CURLOPT_URL, $url);
   curl_setopt($handle, CURLINFO_HEADER_OUT, 1);
   curl_setopt($handle, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux 
   x86_64; rv:55.0) Gecko/20100101 Firefox/55.0.1');
   curl_setopt($handle, CURLOPT_FOLLOWLOCATION, false);
   curl_setopt($handle, CURLOPT_NOSIGNAL, true);
   curl_setopt($handle, CURLOPT_RETURNTRANSFER, false); 
   curl_setopt($handle, CURLOPT_HTTPHEADER, $header); 
   curl_setopt($handle, CURLOPT_HEADER, false);
   curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);

   $result = curl_exec($handle);
   curl_close($handle);
   // var_dump($result);
   print_r($result);

   $channel = 'CH1';// This is a channel you want to watch 
   $list = explode(PHP_EOL, $result);
   for ($i = 0; $i < count($list); ++$i){
   if ((strpos($list[$i], $channel) !== false) && ($i + 1 < count($list))) {
    echo $list[$i+1];
    break;
} 
   }
  ?>

您可以帮我更新我的代码吗? 提前致谢

0 个答案:

没有答案