Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bustarice's avatar

Parsing XML File

I'm trying to parse a file into an array but not sure currently how that may be done.

I'm used to using something such as Guzzle to get a request and into a json/xml response and then process the data from there.

The source of this data does not have any API available only a downloadable file in XML or CSV.

What would be a common method of handling files with data? Do I need to store it in a directory? Upload it to a web host online? Not sure how to go about it just yet.

Appreciate the help ahead of time.

0 likes
7 replies
bustarice's avatar

@RachidLaasri In basic terms yeah I guess that's the idea.

Let me provide more clarity.

So I believe if I was to do this without Laravel it would need to either have a frontend form attach/upload file field then on the backend read it with something like xmlreader and lastly parse it with simplexml and then I can iterate through it and either use in an array and/or dump what I need into MySql and be done with it.

So is there a more Laravel way to do this or....?

tuneless's avatar

Hello @dnamotoring, I would get a data file with Guzzle, store the response-body locally, if I want to use it once more in other aspects.

Then I could use a nice Laravel package like this: https://github.com/orchestral/parser Where I could do this:

$xml = $reader->load('path/to/file_loaded_by_guzzle');
$user = $xml->parse([
    'id' => ['uses' => 'user.id'],
    'email' => ['uses' => 'user.email'],
    'followers' => ['uses' => 'user::followers'],
]);

If this package is buggy or does not all things I want to do, I would search for native php XML parsers and use these Classes.

But upload on a front-end is not necessary I think, if you use Guzzle.

bustarice's avatar

Hi @tuneless ,

Is Guzzle only for mainly rest/soap getting of data into xml/json? I'm familar with using it for that purpose.

My XML data file will need to be download manually by me as the source doesn't provide it through rest/soap api.

So using your example would I simply store it at a location whether locally or on a server somewhere and this parser would then just parse this XML then allowing me at that point to use the data as I see fit?

tuneless's avatar
Level 2

need to be download manually

I just use Guzzle like CURL:

$guzzleClient = new Client();
$response = $guzzleClient->get('YOUR_SOURCE_URL');
$body = $response->getBody();
$body->seek(0);
$size = $body->getSize();
$file = $body->read($size);
File::put('STORAGE_PATH', $file);

// Then you can do stuff with your file locally on your server
$xml = $reader->load('STORAGE_PATH');

Can you download your file via HTTP GET?

1 like
bustarice's avatar

@tuneless ,

Yup I think this will get me on the right path to experiment.

Thanks a lot!

1 like

Please or to participate in this conversation.