synan54 synan54 - 4 months ago 27
PHP Question

Get IFrame src with SimpleHTMLDOM parser

hi i was working on a scrapper but i am unable to get one of information.

this is the link http://sfglobe.com/?id=19110

div class="video_container">
<div class="video_object">
<iframe id="player" width="100%" height="100%" frameborder="0" allowfullscreen="1" title="YouTube video player"
src="http://www.youtube.com/embed/KMYrIi_Mt8A?enablejsapi=1&controls=1&showinfo=0& color=white&rel=0&wmode=transparent&modestbranding=1&theme=light&autohide=1&start=4& origin=http%3A%2F%2Fsfglobe.com">
<!DOCTYPE html>
<html lang="en" data-cast-api-enabled="true" dir="ltr"


i need src ="http://www.youtube.com/embed/KMYrIi_Mt8A....."

i this is my code which does not work

foreach ($html->find('.video_object')as $iframe){
echo "this is video ".$iframe->outertext ." <br>";
}


thank you very uc

Answer

Do this return anything on your code?

$html->find('.video_object iframe')

If so, try using ->getAttribute('src'); it might work.

For further information take a look at PHP DOMElement


EDIT

Use XPath instead, it will output the expected result

//init DOMDocument
$dom = new DOMDocument();
//get the source from the URL
$html = file_get_contents("URL");
//load the html from html string
$dom->loadHTML($html);
//init XPath
$xpath = new DOMXPath($dom);

//fetch the src from the iframe within
$iframe_src=$xpath->query('//*[@class="CLASSNAME"]/iframe//@src');

vardump($iframe_src);