0

I have an XML string obtained as response of SoapClient request (I have also tried with cURL)

I then use:

$xml = simplexml_load_string($result);

I am able to obtain the attributes of a node inside $xml by the node index (Lets say I want to access vehicle node with index 100:

dump($xml->vehicle[100]->attributes());

Or I can access a particular attribute by its name:

dump($xml->vehicle[100]['key']):

When I try to do the same inside a foreach it doesn't work

foreach($xml->vehicle as $car) {
    dump($car->attributes());
}

I have also tried the following without success

$vehicles = array();
foreach($xml->vehicle as $index => $car) {
    $vehicles[$index] = array($car->attributes());
}
dump($vehicles);

I am currently just trying to dump the attributes of each node but my goal is to later create an entry in a Vehicles database and each attribute is a column, the amount of rows depends on the response of SoapClient or cURL

How can I access the attributes of each node inside a foreach?

Thank you in advance for the help

EDIT

I get HTTP error 500 but I have already increased the default socket timeout to 6000 (Just in case haha) but if I remove the foreach then the code completes the execution in around 1 min

HeytalePazguato
  • 290
  • 4
  • 13
  • what is the error you get? – GoDLighT Nov 13 '21 at 05:28
  • Ah sorry I forgot to include that in the description: I get HTTP error 500 but I have already increased the default socket timeout to 6000 (Just in case haha) but if I remove the foreach then the code completes the execution in around 1 min – HeytalePazguato Nov 13 '21 at 05:31
  • http 500 is error that say something is wrong with the server, try and get more specific details about what is wrong, can you try and warp the loop in try and dump the error that you get? – GoDLighT Nov 13 '21 at 05:36
  • Thanks for the advice, I have been trying all day to test it with a try and catch but even if I have all the code inside a try it stills gets HTTP 500 when the foreach is in the code, when I comment it out then the code works, something is not working with the foreach – HeytalePazguato Nov 13 '21 at 15:39
  • foreach($xml->vehicle as $car) { dump($car->attributes()); } did you try only the loop do nothing inside? – GoDLighT Nov 13 '21 at 16:13
  • Yes I made several tries all day, using only the foreach inside of the try, then it kept failing with 500, I kept adding things into the try so maybe something comes out from the exception but keeps returning error 500, the last try I made was with all the code inside the try and still returned error 500 – HeytalePazguato Nov 13 '21 at 16:53
  • if you var dump the `$xml->vehicle` what do you get? – GoDLighT Nov 13 '21 at 17:35
  • If I just dump the $xml->vehicle I get the content of all nodes, it is a string, but I don't get the attributes. I tested to increase the timeout to 60000 and leave it run overnight, in the morning I found all the results so it works! But it seems it is just too long, I am still wondering why it didn't work with 6000. So in conclusion, it seems it is a matter of timeout but it is strange that try/catch didn't get any exception message – HeytalePazguato Nov 14 '21 at 06:31
  • it’s strange that it’s happening only when you loop over the data, ‘simplexml_load_string’ is getting complete string so where this timeout is getting from? do you change the php run timeout? or the soap client timeout? – GoDLighT Nov 14 '21 at 07:55

0 Answers0