Using SimpleXml in PHP and converting to an Object
I am struggling a bit with getting the values from my XML feed using Simplexml.
I am using this to get the XML into an object:
$this->adObj = simplexml_load_string(file_get_contents($url));
However, I am struggling to get the values.
For example, these are actually product feeds from a 3rd party. When I use var_dump I can see $obj->Products->Product which is actually an array of products but if I try and get the values from the first array I get a blank page.
$obj->Products->Product[0]->Offer->Url
and this returns a blank page?
Here is a snipplet of the var_dump. I think I am making a mistake somewhere when I select a specific array using "[0]". Any help or advice appreciated.
object(SimpleXMLElement)#2 (2) {
["Products"]=>
object(SimpleXMLElement)#3 (2) {
["@attributes"]=>
array(3) {
["firstResultPosition"]=>
string(1) "1"
["totalResultsAvailable"]=>
string(5) "31576"
["totalResultsReturned"]=>
string(3) "100"
}
["Product"]=>
array(100) {
[0]=>
object(SimpleXMLElement)#5 (2) {
["@attributes"]=>
array(1) {
["type"]=>
string(5) "Offer"
}
["Offer"]=>
object(SimpleXMLElement)#105 (14) {
["@attributes"]=>
array(1) {
["id"]=>
string(32) "0edea796f13547724c949f93247ca113"
}
["Url"]=>
string(270) "http://uk.shoppingapis.kelkoo.com/ctl/go/sitesearchGo?.ts=1267485724046&.sig=eZJrCjf4u03RufQ0DcS7Q.zyeZ4-&offerId=0edea796f13547724c949f93247ca113&searchId=87248117221_1267485724041_24181632&affiliationId=96928412&country=uk&wait=true&ecs=ok&comId=532601&catId=100020213"
Shaggy posted this at 21:30 — 2nd March 2010.
They have: 121 posts
Joined: Dec 2008
It's been a while...
$obj->Products->Product[0]->Offer["Url"]
??
If not, and you're still stuck, post the XML and I'll try to dust off PHP...
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.