Take a little look at this little code block:
$response = <<< XMLBLOCK<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><GeteBayOfficialTimeResponse xmlns="urn:ebay:apis:eBLBaseComponents"><Timestamp>2005-10-28T01:01:04.668Z</Timestamp><Ack>Success</Ack><Version>429</Version><Build>e429_intl_Bundled_1949355_R1</Build></GeteBayOfficialTimeResponse></soapenv:Body></soapenv:Envelope> XMLBLOCK; $xml = simplexml_load_string($response);Now, imagine that for some crazy reason, you care what time it is in eBay land, how would you access that Timestamp value? Your first thought is probably something along the lines of:
echo "Time: " . $xml->Envelope->Body->GeteBayOfficialTimeResponse->Timestamp . "\n";Quaint, nice try, but you're not even close. If you're really paying attention you noticed the namespaces and might try one of these:
echo "Time: " . $xml->{'soapenv:Envelope'}->{'soapenv:Body'}->GeteBayOfficialTimeResponse-> Timestamp . "\n"; echo "Time: " . $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body-> GeteBayOfficialTimeResponse->Timestamp . "\n";Or noticing that the body is also in the namespace, this:
echo "Time: " . $xml->children('http://schemas.xmlsoap.org/soap/envelope/')-> children('http://schemas.xmlsoap.org/soap/envelope/')->GeteBayOfficialTimeResponse-> Timestamp . "\n";Nope, It's this:
echo "Time: " . $xml->children('http://schemas.xmlsoap.org/soap/envelope/')-> children('urn:ebay:apis:eBLBaseComponents')->GeteBayOfficialTimeResponse->Timestamp . "\n";In retrospect, it seems to make a lot of sense. However, all the documentation I've read on namespaces in SimpleXML tell you that you need to pass it the URL of the relevant namespace (the aliases are relatively irrelevant). It would be great if the print_r() or var_dump() output gave you some deep indication of how the data should be accessed, but alas, it does not:
SimpleXMLElement Object ( [Body] => SimpleXMLElement Object ( [GeteBayOfficialTimeResponse] => SimpleXMLElement Object ( [Timestamp] => 2005-10-28T01:01:04.668Z [Ack] => Success [Version] => 429 [Build] => e429_intl_Bundled_1949355_R1 ) ) )