0

我正在尝试从 URL 中提取 XML 数据。它不是 XML 文件,而是基于请求字符串的响应。

$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>";
$xml = simplexml_load_string($url); // requires allow_url_fopen to be on
$elg = (string)$xml->Property[Eligibility];

var_dump($xml);

我想要的数据如下所示:

<Eligibility>
    <Adjusted AnnualIncome="" TotalDeduction="" AdjustedIncome="" ElderlyDeduction="" YoungDeduction="">
    </Adjusted>
    <Section502Guaranted MaximumAdjusted="" Eligible="">
    </Section502Guaranted>
    <Section502Direct MaximumAdjusted="" Eligible="">
    </Section502Direct>
    <Property Eligibility="UnableToVerifyAddress" MapURL="http://rdgdwe.sc.egov.usda.gov/eligibilitymaps/index.jsp?app=RBSIELG&ADDRESS=392 Spruce east alton&STATE=IL&ZIP=62024"/>
    <ErrorResponse EngineId="" HostName="" MaxSeverity="" LogFile="" Class="" Module="" Severity="" Time="">
        <Message Code="" Type="" Text=""/>
    </ErrorResponse>
</Eligibility>

我回来的样子是这样的:

bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) bool(false) 

我不知道它为什么这样做。也没有错误发布。

4

3 回答 3

2

simplexml_load_string()需要格式正确的 XML 字符串,而不是 URL。你可能想试试simplexml_load_file()

于 2013-03-21T21:36:58.647 回答
0

尝试这个:

$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>";
$url_arr = explode( 'requestString=', $url );
$xml = simplexml_load_string( $url_arr[ 1 ] ); // requires allow_url_fopen to be on
$elg = (string)$xml->Property[Eligibility];

var_dump($xml);

基本上,我只是分解你给出的 $url 以便格式良好的 xml 将被隔离在 $url_arr[ 1 ]

于 2013-03-21T21:40:03.153 回答
0

您需要从 $url 中删除以下内容:

http://eligibility.sc.egov.usda.gov/eligibility/eligibilityservice?eligibilityType=Property&requestString=

因为 simplexml_load_string 函数采用格式正确的 XML 字符串,并且您在起始标记之前有一个 URL

于 2013-03-21T21:38:42.957 回答