【问题标题】:How to get video url using file_get_contents in php如何在 php 中使用 file_get_contents 获取视频 url
【发布时间】:2016-09-15 12:41:42
【问题描述】:

我正在尝试从其他网站获取视频和海报 让我们称之为 othersite.com

其他 site.com 有这个参数:

<div class="player">
    <div id="l_player" class='l-player' >
        <noscript>
            <video style="width:100%; height:100%;"
               controls="controls"
               autobuffer="autobuffer"
               class="player-html5"
               preload="metadata"
               poster="http://othersite.com/img.jpg">
                <source src="http://othersite.com/video.mp4" type="video/mp4">
            </video>
        </noscript>
    </div>  

使用下面的 php,我从 opengraph 标记中获取了图像,但我试图获取我还无法获取的 mp4 url​​

$Agent  = array('http' => array('user_agent' => 'Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D)'));
$String = file_get_contents('http://www.othersite.com/'. $video .'/', false, stream_context_create($Agent) );
preg_match("/<source src=\"(.*)\" controls type=\"video\/mp4\"/", $String, $VideoURL);
$VideoURL = $VideoURL[1];
$IMG = getstring($String,'<meta property="og:image" content="','"');

我的 php 正在获取图像,只有你知道我在 preg_match 中做错了什么以及如何获得http://othersite.com/video.mp4

【问题讨论】:

  • 您的来源中没有controls。您也不应该使用正则表达式来解析 HTML/XML。

标签: php preg-match file-get-contents


【解决方案1】:

这可以通过以下方式使用 Preg_Match 来完成。它非常简单,而且效果非常好!

$source = file_get_contents("That Page");

preg_match("'<source src=\"(.*?)\" type=\"video/mp4\">'si", $source, $match);
if($match) echo "result=".$match[1];

这应该做你想做的。

在最近发表评论后,我觉得我还应该补充说 si 修饰符使这个正则表达式变得更松散。

【讨论】:

  • 解决了 FluxCoder 你是伟大的我的朋友,我从早上就开始使用它,因为它已经准备好解决这个问题,非常感谢。你有任何高级链接来学习这个逻辑吗?
  • 答案应该有解释。 si 修饰符使这个正则表达式更加松散。
  • 不是真的,我很想私下帮助你,但我真的不知道你会如何做到这一点,如果没有在 Skype 上添加我:FluxCoder
  • @Gazi 正则表达式逻辑或...? regular-expressions.info 有很多信息,regex101.com 非常适合测试正则表达式并了解它如何/为什么起作用。这是您的第一个正则表达式regex101.com/r/aT0fW3/1,正如您在regex101.com/r/aT0fW3/2 中看到的那样,只需删除controls 即可解决问题。我会更改您的分隔符和引号用法。这也会减少你的逃跑次数。
  • @Gazi 这只是来自知识和过去的经验。
猜你喜欢
  • 2015-07-16
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多