I'm trying to parse a response from the Ontraport API, which is returned in an ugly XML format.
<result>
<contact id="1" date="1424746532" dlm="1425357692" score="0.00" purl="" bulk_mail="1">
<Group_Tag name="Contact Information">
<field name="Company">Test.com</field>
<field name="Email">test@test.com</field>
<field name="Group"/>
<field name="Specialty"/>
<field name="User ID"/>
<field name="Display First"/>
<field name="Display Last"/>
</Group_Tag>
</contact>
</result>
Im using SimpleXML
and the simplexml_load_string
function. When I var_dump the response from that function I get the following output:
object(SimpleXMLElement)#1 (1) {
["contact"]=>
object(SimpleXMLElement)#2 (2) {
["@attributes"]=>
array(6) {
["id"]=>
string(1) "1"
["date"]=>
string(10) "1424746532"
["dlm"]=>
string(10) "1425357692"
["score"]=>
string(4) "0.00"
["purl"]=>
string(0) ""
["bulk_mail"]=>
string(1) "1"
}
["Group_Tag"]=>
object(SimpleXMLElement)#3 (2) {
["@attributes"]=>
array(1) {
["name"]=>
string(19) "Contact Information"
}
["field"]=>
array(7) {
[0]=>
string(8) "Test.com"
[1]=>
string(13) "test@test.com"
[2]=>
object(SimpleXMLElement)#4 (1) {
["@attributes"]=>
array(1) {
["name"]=>
string(5) "Group"
}
}
[3]=>
object(SimpleXMLElement)#5 (1) {
["@attributes"]=>
array(1) {
["name"]=>
string(9) "Specialty"
}
}
[4]=>
object(SimpleXMLElement)#6 (1) {
["@attributes"]=>
array(1) {
["name"]=>
string(7) "User ID"
}
}
[5]=>
object(SimpleXMLElement)#7 (1) {
["@attributes"]=>
array(1) {
["name"]=>
string(13) "Display First"
}
}
[6]=>
object(SimpleXMLElement)#8 (1) {
["@attributes"]=>
array(1) {
["name"]=>
string(12) "Display Last"
}
}
}
}
}
}
How could I retrieve the Company and Email values, or any specific field from there, which I don't know if is empty or has a value.
I don't see the field name for those with a value and I can't assume the fields order.
Edit: I don't think this is a duplicate as @Rizier123 said, as I'm trying to retrieve an item based on an element attribute, and I'm not even getting on the var_dump the attributes for the fields that have a value. Thus, the proposed and accepted solutions for that other question don't apply here. As he asked, I'm adding the full and real code that I'm using to test this:
$response = '<result>
<contact id="1" date="1424746532" dlm="1425357692" score="0.00" purl="" bulk_mail="1">
<Group_Tag name="Contact Information">
<field name="Company">Test.com</field>
<field name="Email">test@test.com</field>
<field name="Group"/>
<field name="Specialty"/>
<field name="User ID"/>
<field name="Display First"/>
<field name="Display Last"/>
</Group_Tag>
</contact>
</result>';
$responseData = simplexml_load_string($response);
var_dump($responseData);