首页 > 解决方案 > iframe 跨域框架错误显示

问题描述

我正在使用 WordPress 进行开发,我的控制台面板将错误消息显示为

未捕获的 DOMException:阻止具有源“ https://demo.utrop.no ”的框架访问跨域框架。

当我访问 iframe 中的内容时会发生这种情况,下面的代码将显示我真正想要的内容。从这个 iframe 中,我需要获取正在加载到 WordPress 中 iframe 中的显示原始按钮的“a”标签详细信息。

<iframe src="" style="width: 400px; height: 200px;" id="myFrame" name="iframe_a"></iframe>

<a href="https://www.retriever-info.com/go/?a=79334&d=00233220191223303391398&p=1861654&s=2332&sa=2044493&x=fe0e0a14ee1e6ad2107d665284c5082c" target="iframe_a">W3Schools.com</a>

<input type="button" id="btn" value="click" onclick="myFunction()">

<?php
    global $wpdb;
    $url = 'https://www.retriever-info.com/go/?a=79334&d=00233220191223303391398&p=1861654&s=2332&sa=2044493&x=fe0e0a14ee1e6ad2107d665284c5082c'; 
    $ch = curl_init($url);
    curl_setopt($ch,
        CURLOPT_HTTPHEADER,
        array(

            'Content-Type:application/xml',
            'Authorization: 79334',
        )
    );
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);

    $html = curl_exec($ch); 
    $redirectedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    echo "Original: $url <br>Final: $redirectedUrl";
    $opts = array(
        'http' => array(
            'method'=>"GET",
            'header'=>"Content-Type: text/html; charset=utf-8"
        )
    );



    $ch = curl_init();
    curl_setopt($ch,
        CURLOPT_HTTPHEADER,
        array(

            'Content-Type:application/xml',
            'Authorization: 79334',
        )
    );

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 50);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );

     $html = curl_exec($ch); 




        $dom = new DOMDocument();
        @$dom->loadHTML($html);
        $xpath = new DOMXPath($dom);
        $href = $xpath->evaluate("/html/body//a");
        echo '<pre>';
            print_r($html);
        echo '</pre>';
        for ($i = 0; $i < $href->length; $i++) {
            $data = $href->item($i);
            $url = $data->getAttribute('href');

            echo "Successful Link Harvest: ".$url.'<br>';
        }

      curl_close($ch);

   ?>





<script>

    function myFunction() {
      var iframe = document.getElementById("myFrame");
      var elmnt = iframe.contentWindow.document.getElementsByTagName("a")[0];
      console.log(elmnt)

</script>

标签: phpwordpresscurliframe

解决方案


推荐阅读