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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何从远程页面获取iframe内容?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我认为 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>

但是我使用Simple 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();

但很明显,没有返回任何内容;(因为在运行PHP之后会显示iframe;(

所以,我在想我可能需要注入这段代码

<script type="text/javascript">

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

</script>

在我抓取的页面标题中存储在$html中.

任何想法如何获得这样的iframe内容,或者这种过于复杂的方法和一些更简单解决方案确实存在?

解决方法

由于原始策略相同,您将无法使用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);

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何从远程页面获取iframe内容?全部内容,希望文章能够帮你解决php – 如何从远程页面获取iframe内容?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。