simplexml_load_string无法从URL加载xml

时间:2013-03-21 21:33:44

标签: php

我正在尝试从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) 

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

3 个答案:

答案 0 :(得分:2)

simplexml_load_string()需要格式良好的XML字符串,而不是URL。您可能想尝试simplexml_load_file()

答案 1 :(得分:0)

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

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

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

答案 2 :(得分: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,以便在$ url_arr中隔离格式良好的xml [1]