How to get iframe content from remote page?

I think PHP is useless, bacause iframe inserted after php execution, or am I mistaken?

So, the only solution I know about is using Javascript / jQuery.

eg. this will work if JS is on the same page as the 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, I use the Simple HTML DOM library to capture a remote web page, for example:

 $url = 'http://page-with-some-iframe.com/'; $html = file_get_html( $url ); // Find iframes and put them in an array $iframes_arr = array(); foreach($html->find('iframe') as $element) { $iframes_arr[] = $element->outertext; } var_dump($iframes_arr); die(); 

But obviously, nothing is returned: (, because iframes are displayed after php starts; (

So, I thought that I probably would need to enter this code:

 <script type="text/javascript"> $(function() { var myContent = $("#iFrame").contents().find("#myContent") }); </script> 

in the header of my captured page stored in $ html.

Any idea how to get iframe content like this, or is this an overly complicated approach, and is there a simpler solution?

+4
source share
2 answers

You will not be able to access the remote page document inside the iframe using javascript due to the same origin policy.

If you know the URL of the page, you can use PHP CURL to get it

 <?php // Initialise a cURL object $ch = curl_init(); // Set url and other options curl_setopt($ch, CURLOPT_URL, "http://page-with-some-iframe.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Get the page contents $output = curl_exec($ch); // close curl resource to free up system resources curl_close($ch); 
+4
source

This is not possible due to security policy. If you can get the contents of a remote iframe, you can easily steal someone on facebook or bank data. This is why all web browsers block this.

+1
source

Source: https://habr.com/ru/post/1481928/


All Articles