Search code examples
phppreg-matchfile-get-contents

How to get video url using file_get_contents in php


Im trying to get video and poster from another site Lets call it othersite.com

other site.com has this parameters:

<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>  

with the below php I got the image from the opengraph tag but Im trying to get the mp4 url which I couldnt get yet

$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="','"');

my php is getting the image only do you have any idea what Im doing wrong in preg_match and how to get the http://othersite.com/video.mp4


Solution

  • This can be done by using Preg_Match in the following way. It's pretty simple and it works extremely well!

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

    This should do what you want it to.

    After a recent comment, I felt that I should also add that The si modifiers are making this regex looser.