php - How to access XML or transform to JSON in Laravel

763

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 ...

354

Answer

Solution:

Try this:

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

dump($array);
830

Answer

Solution:

I solve my problem with:

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

People are also looking for solutions to the problem: php - Has Laravel 5.4 a solution to log jobs done?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.