php - How to get iframe content from a remote page? -
i think php useless, bacause iframe inserted after php executed, or wrong?
so, solution aware of use javascript/jquery.
e.g. work if js on same page iframe:
<html> <head> <title></title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script> <script type="text/javascript"> $(function() { var mycontent = $("#iframe").contents().find("#mycontent") }); </script> </head> <body> <iframe src="mifile.html" id="iframe" style="width:200px;height:70px;border:dotted 1px red" frameborder="0"> <div id="mycontent"> iframe content blablabla </div> </iframe> </body> </html>
however using simple html dom library grab distant webpage like:
$url = 'http://page-with-some-iframe.com/'; $html = file_get_html( $url ); // find iframes , put them in array $iframes_arr = array(); foreach($html->find('iframe') $element) { $iframes_arr[] = $element->outertext; } var_dump($iframes_arr); die();
but obviously, nothing returned ;(, because iframes displayed after php run ;(
so, thinking need inject code:
<script type="text/javascript"> $(function() { var mycontent = $("#iframe").contents().find("#mycontent") }); </script>
in header of grabbed page stored in $html.
any idea how iframe content or complicated approach , easier solution exist?
you won't able access document of remote page inside iframe using javascript because of same origin policy.
if know url of page can use php curl retrieve it
<?php // initialise curl object $ch = curl_init(); // set url , other options curl_setopt($ch, curlopt_url, "http://page-with-some-iframe.com"); curl_setopt($ch, curlopt_returntransfer, 1); // page contents $output = curl_exec($ch); // close curl resource free system resources curl_close($ch);
Comments
Post a Comment