Plummer
Plummer

Reputation: 6698

Putting JSON data into a variable in PHP

EDIT: I don't think the final output looks like JSON. If not, what is it?

I have this code:

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

Which out puts this array:

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""

I just want to grab the string(12) ""InEligible"" portion. How would I put this into a variable?

Upvotes: 0

Views: 207

Answers (2)

Fabian Schmengler
Fabian Schmengler

Reputation: 24576

EDIT: I don't think the final output looks like JSON. If not, what is it?

string(12) ""InEligible""

That's JSON. A string in JSON is enclosed in quotes, no more, no less.

Upvotes: 0

Tim Ebenezer
Tim Ebenezer

Reputation: 2724

From your code there $elg already holds this value, and it is a variable.

Upvotes: 1

Related Questions