我有一个非常混乱的东西,可以深入到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&ADDRESS=7865 ILLINOIS CASEYVILLE&STATE=IL&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')
获得的结果只是“未定义”。
答案 0 :(得分:1)
您需要使用JS以正确的方式访问iframe并使用XML DOM:
var f = $('.frame')[0];
var Property = f.contentDocument.getElementsByTagName('Property')[0];
var Eligibility = Property.getAttribute('Eligibility');
或类似的东西。注意:只有域,端口和协议匹配时才有可能! (所有者文档和iframe文档。)
编辑啊,我现在看到iframe的网址很可能与您网站的网址不同。比这还没有工作。 Javascript(浏览器)不允许跨站点脚本。出于非常好的安全原因。
您需要使用PHP读取XML。更容易:
$xml = simplexml_load_file($url); // requires allow_url_fopen to be on
$Eligibility = (string)$xml->Property['Eligibility'];
(没有测试过那个。)