编辑:我不认为最终输出看起来像 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""
部分。我如何将其放入变量中?