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

9
  • what is the error you get? Commented Nov 13, 2021 at 5: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 Commented Nov 13, 2021 at 5: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? Commented Nov 13, 2021 at 5: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 Commented Nov 13, 2021 at 15:39
  • foreach($xml->vehicle as $car) { dump($car->attributes()); } did you try only the loop do nothing inside? Commented Nov 13, 2021 at 16:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.