代码之家  ›  专栏  ›  技术社区  ›  norcal johnny

使用PHP获取并返回媒体url(m3u8)

  •  0
  • norcal johnny  · 技术社区  · 9 年前

    我有一个网站,从客户托管视频。在网站上,文件通过m3u8链接从外部加载。

    客户现在希望在Roku频道上播放这些视频。

    如果我只是从网站上使用m3u8链接,它会给出一个错误,因为生成的url是与cookie一起发送的,因此客户端必须单击该链接以为其生成新代码。

    我想,如果可能的话(我在这里没有看到这一点),是抓取html页面,然后通过网站上的PHP脚本从Roku返回链接。

    我知道如何使用纯php获取标题等,但在返回m3u8链接时遇到问题。。

    我确实有代码来表明我不是在找讲义,而是在尝试。

    例如,这就是我用来获取标题名的方法。

    注意:我想知道是否有可能有一个php自动填充每个url的html页面,这样我就不必为每个预键入url的视频使用不同的php。

    <?php
    $html = file_get_contents('http://example.com'); //get the html returned from the following url
    
    $movie_doc = new DOMDocument();
    
    libxml_use_internal_errors(TRUE); //disable libxml errors
    
    if(!empty($html)){ //if any html is actually returned
    
        $movie_doc->loadHTML($html);
        libxml_clear_errors(); //remove errors for yucky html
    
        $movie_xpath = new DOMXPath($movie_doc);
    
        //get all the titles
        $movie_row = $movie_xpath->query('//title');
    
        if($movie_row->length > 0){
            foreach($movie_row as $row){
                echo $row->nodeValue . "<br/>";
            }
        }
    }
    ?>
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   norcal johnny    8 年前

    有一种简单的方法,涉及使用正则表达式。

    在本例中,假设视频M3u8文件位于: http://example.com/theVideoPage

    您可以将XML中的视频URL源指向PHP文件。

    http://thisPhpFileLocation.com

    <?php
    $html = file_get_contents("http://example.com/theVideoPage");
    
    preg_match_all(
        '/(http.*m3u8)/',
    
        $html,
        $posts, // will contain the article data
        PREG_SET_ORDER // formats data into an array of posts
    );
    
    foreach ($posts as $post) {
        $link = $post[0];
    
    header("Location: $link");
    }
    ?>
    

    现在,如果你想使用一个URL,你可以在末尾附加一个URL链接,它可能看起来像这样,你可以使用一个地址作为视频URL的地址

    http://thisPhpFileLocation.com?id=theVideoPage

    <?php
    $id = $_GET['id'];
    $html = file_get_contents("http://example.com".$id);
    
    preg_match_all(
        '/(http.*m3u8)/',
    
        $html,
        $things, // will contain the article data
        PREG_SET_ORDER // formats data into an array of posts
    );
    
    foreach ($things as $thing) {
        $link = $thing[1];
    
    // clear out the output buffer
    while (ob_get_status())
    {
        ob_end_clean();
    }
    
    // no redirect
    header("Location: $link");
    
    }
    ?>
    
    推荐文章