0

I am trying to extract the string value from the SimpleXMLElement below (which has been returned from a SOAP endpoint) but I'm having no joy:

object(SimpleXMLElement)[476]
  public 'return' => string 'ff7ecc8af5ecaaba412c3b453c5f65f1' (length=32)

I have tried casting the entire object as a string, it just returns empty, I've tried treating 'return' as key, etc. This is such a simple task, can't believe it's got me stumped.

HomerPlata
  • 1,687
  • 5
  • 22
  • 39

2 Answers2

1

Your problem, somewhat hidden in the question, is that the element name is a reserved word, so you can't use the ordinary syntax:

$value = (string)$xml->return; # SYNTAX ERROR

The solution is to use braces and quotes around the name, which allows you to use reserved words or characters:

$value = (string)$xml->{'return'};
IMSoP
  • 89,526
  • 13
  • 117
  • 169
0

Try adding (string) in front of the variable. For example:

echo (string)$xml->fieldname;

The SimpleXMLElement stuff is very object-oriented but as you can see from the debug output you shared, the string is in there somewhere.

Lorna Mitchell
  • 1,819
  • 13
  • 22
  • Thanks Lorna. The problem is, I don't have a fieldname. I tried to use 'return' as a fieldname, but can't because it's a reserved word. – HomerPlata Apr 13 '17 at 09:30
  • Ah, totally missed that the reserved word was causing the issue! @IMSoP spotted it though, awesome. – Lorna Mitchell Apr 19 '17 at 09:53