Aleks Per
Aleks Per

Reputation: 1639

How to access XML or transform to JSON in Laravel

I have this XML response:

<AuthorisationResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://webservice.redletterdays.co.uk/">
<RequestSuccessful>true</RequestSuccessful>
<ErrorMessage/>
<Token>3d3d4a94-87fe-414c-ae0a-fd0e0c4c0060</Token>
</AuthorisationResult>

enter image description here

That's what I got when I write:

return $res;

How can I access Token?

I try :

return $res->Token; 

and also

return $res->children();

but this code returns me errors...

SO, How I can access Token in XML or how to transform into JSON and easy access to Token like $res->Token ...

Upvotes: 0

Views: 1065

Answers (2)

Aleks Per
Aleks Per

Reputation: 1639

I solve my problem with:

$res = json_encode(simplexml_load_string($res->getBody()));
$res = json_decode($res);
$token = $res->Token;

Upvotes: 1

Sapnesh Naik
Sapnesh Naik

Reputation: 11646

Try this:

$json = $res->getBody();
$array = json_decode($json);

dump($array);

Upvotes: 1

Related Questions