如何从远程页面获取iframe内容?

4 php iframe jquery parsing simple-html-dom

我认为PHP没有用,因为执行php后会插入iframe,还是我错了?

因此,我知道的唯一解决方案是使用Javascript / jQuery。

例如,如果JS与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>
Run Code Online (Sandbox Code Playgroud)

但是我正在使用简单HTML DOM库来抓取遥远的网页,例如:

$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();
Run Code Online (Sandbox Code Playgroud)

但很明显,什么也不会返回;(,因为在php运行后会显示iframe;(

所以,我在想我可能需要注入以下代码:

<script type="text/javascript">

  $(function() {
    var myContent = $("#iFrame").contents().find("#myContent")
  });

</script>
Run Code Online (Sandbox Code Playgroud)

在我的抓取页面的标题中(存储在$ html中)。

是否知道如何获取类似的iframe内容,还是这种方法过于复杂且存在一些更简单的解决方案?

mic*_*ael 5

由于相同的原始政策,您将无法使用javascript访问iframe内的远程页面的文档。

如果您知道页面的URL,则可以使用PHP CURL检索它

<?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);  
Run Code Online (Sandbox Code Playgroud)