0

我有一个大杂烩,它深入到 iframe 中以从 XML 结果中检索属性。(我这样做是为了避免从这个异地服务器获取数据所涉及的十亿个其他问题。)

<?php

    $link = mysqli_connect(//ALL THE CONNECTS!);
    mysqli_select_db(//THE_DB, $link);

    $query = "SELECT * FROM jos_mls AS mls
                INNER JOIN jos_activeagents AS active ON mls.MSTLISTBRD = active.AGENTUID
                LIMIT 10;";

    $result = mysqli_query($query);

    $array = array(); 
    $index = 0;

    while($row = mysqli_fetch_array($result))
    {
        $array[$index] = $row;
        $index++;
    }

    foreach ($array as $key => $value) {
        $mls = $value[1];
        $street = $value[5].' '.$value[6];
        $city = $value[9];
        $state = $value[10];
        $zip = $value[11];
        $url = "http://eligibility.sc.egov.usda.gov/eligibility/eligibilityservice?eligibilityType=Property&requestString=<?xml version='1.0'?><Eligibility xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='/var/lib/tomcat5/webapps/eligibility/Eligibilitywsdl.xsd'><PropertyRequest StreetAddress1='".$street."' StreetAddress2='' StreetAddress3='' City='".$city."' State='".$state."' County='' Zip='".$zip."' Program='RBS'></PropertyRequest></Eligibility>";
        $frame = '<iframe class="frame" mls="'.$mls.'" style="width: 10px; height: 10px;" src="'.$url.'"></iframe>';
        echo $frame;
    }

    mysql_close($link);
?>
<div id="test"></div>
<script type="text/javascript">
    $(document).ready(function(){
        $('.frame').each(function(){
            var mls = $(this).attr('mls'),
                usda = $(this).contents().find('Property').attr('Eligibility');
            $('#test').append(mls+' '+usda+'<br/>');
        });
    });
</script>

iframe 中的数据如下所示...

<?xml version="1.0" encoding="UTF-8"?>
<Eligibility xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Adjusted AnnualIncome="" TotalDeduction="" AdjustedIncome="" ElderlyDeduction="" YoungDeduction="">
        </Adjusted>
        <Section502Guaranted MaximumAdjusted="" Eligible="">
        </Section502Guaranted>
        <Section502Direct MaximumAdjusted="" Eligible="">
        </Section502Direct>
        <Property Eligibility="InEligible" MapURL="http://rdgdwe.sc.egov.usda.gov/eligibilitymaps/index.jsp?app=RBSIELG&amp;ADDRESS=7865 ILLINOIS CASEYVILLE&amp;STATE=IL&amp;ZIP=62232" />
        <ErrorResponse EngineId="" HostName="" MaxSeverity="" LogFile="" Class="" Module="" Severity="" Time="">
                <Message Code="" Type="" Text="" />
        </ErrorResponse>
</Eligibility>

我需要节点 的Eligibility属性...更新对不起,过早地点击发送。我现在得到的结果只是“未定义”。Property$(this).contents().find('Property').attr('Eligibility')

4

1 回答 1

1

您需要使用 JS 以正确的方式访问 iframe 并使用 XML DOM:

var f = $('.frame')[0];
var Property = f.contentDocument.getElementsByTagName('Property')[0];
var Eligibility = Property.getAttribute('Eligibility');

或类似的东西。当心:这只有在域、端口和协议匹配的情况下才有可能!(所有者文件和 iframe 文件。)

编辑啊,我现在看到 iframe 的 URL 很可能与您网站的不同。比这行不通。Javascript(浏览器)不允许跨站点脚本。出于非常好的安全原因。

您需要使用 PHP 阅读 XML。也容易得多:

$xml = simplexml_load_file($url); // requires allow_url_fopen to be on
$Eligibility = (string)$xml->Property['Eligibility'];

(没有测试那个。)

于 2013-03-20T19:46:25.243 回答