0

编辑:我不认为最终输出看起来像 JSON。如果不是,那是什么?

我有这个代码:

$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);

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

var_dump($elg);

其中输出了这个数组:

object(SimpleXMLElement)#1 (1) {
    ["PropertyRequest"]=> object(SimpleXMLElement)#2 (1) {
        ["@attributes"]=> array(8) {
            ["StreetAddress1"]=> string(13) "7865 ILLINOIS" 
            ["StreetAddress2"]=> string(0) "" 
            ["StreetAddress3"]=> string(0) "" 
            ["City"]=> string(10) "CASEYVILLE" 
            ["State"]=> string(2) "IL" 
            ["County"]=> string(0) "" 
            ["Zip"]=> string(5) "62232" 
            ["Program"]=> string(3) "RBS" 
        } 
    } 
} 
string(12) ""InEligible""

我只想抓住那string(12) ""InEligible""部分。我如何将其放入变量中?

4

2 回答 2

1

从您的代码中$elg已经保存了这个值,它是一个变量。

于 2013-03-22T14:38:39.383 回答
0

编辑:我不认为最终输出看起来像 JSON。如果不是,那是什么?

string(12) ""InEligible""

那是JSON。JSON 中的字符串用引号括起来,不多也不少。

于 2013-03-22T14:48:15.887 回答