1

I have an XML string like below getting returned from an API call

<Results recordReturn="1"><Result><cst_key>1234567-9087-1234-1234-1234567890</cst_key><username>577155</username><ind_first_name>TestingUserFirstName</ind_first_name><ind_last_name>TestUserLastName</ind_last_name><ind_full_name_cp>TestingUserFirstName TestUserLastName</ind_full_name_cp><cst_web_login>xyz@gmail.com</cst_web_login><cst_eml_address_dn>xyz@gmail.com</cst_eml_address_dn><adr_city_state_code>New York, NY  12345</adr_city_state_code></Result></Results>

How do I get the value for cst_key

I am doing the following but it does not gives me any values

$results = simplexml_load_string($stringXml);
echo $results->Results->Result['cst_key'];

After the simplexml_load_string call it seems to be losing the values in it.

pal4life
  • 3,210
  • 5
  • 36
  • 57

1 Answers1

0
$xml = '<Results recordReturn="1"><Result><cst_key>1234567-9087-1234-1234-1234567890</cst_key><username>577155</username><ind_first_name>TestingUserFirstName</ind_first_name><ind_last_name>TestUserLastName</ind_last_name><ind_full_name_cp>TestingUserFirstName TestUserLastName</ind_full_name_cp><cst_web_login>xyz@gmail.com</cst_web_login><cst_eml_address_dn>xyz@gmail.com</cst_eml_address_dn><adr_city_state_code>New York, NY  12345</adr_city_state_code></Result></Results>';


$results = simplexml_load_string($xml);
echo $results->Result->cst_key;

You need to echo only an object, there's no need of ['whatever']; You will need [] to output the first or the second appearance of a tag name (if there are more with the same name)

You also don't need to name the envelope (Results in your case)

baao
  • 71,625
  • 17
  • 143
  • 203