From cc103eeed6b2bc217572125646d23e75e397a6e8 Mon Sep 17 00:00:00 2001 From: jamiematrix <58532601+jamiematrix@users.noreply.github.com> Date: Thu, 2 Jan 2020 09:31:44 +0000 Subject: [PATCH] Added XML support Default set to JSON, but there's now support if the endpoint is returning XML. Currenly XML attributes are not returned (other than from the parent node), but that's next on the list. --- src/twigextensions/FetchTwigExtension.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/twigextensions/FetchTwigExtension.php b/src/twigextensions/FetchTwigExtension.php index d47ffc8..739f807 100644 --- a/src/twigextensions/FetchTwigExtension.php +++ b/src/twigextensions/FetchTwigExtension.php @@ -33,7 +33,7 @@ public function getFunctions() ]; } - public function fetch($client, $method, $destination, $request = [], $parseJson = true) + public function fetch($client, $method, $destination, $request = [], $format = 'json') { $client = new \GuzzleHttp\Client($client); @@ -41,10 +41,15 @@ public function fetch($client, $method, $destination, $request = [], $parseJson $response = $client->request($method, $destination, $request); - if ($parseJson) { + if ($format == 'json') { $body = json_decode($response->getBody(), true); + } elseif ($format == 'xml') { + $xmlbody = simplexml_load_string($response->getBody(), null, LIBXML_NOCDATA); + + $json = json_encode($xmlbody); + $body = json_decode($json, true); } else { - $body = (string)$response->getBody(); + $body = (string)$response->getBody(); } return [ @@ -59,7 +64,8 @@ public function fetch($client, $method, $destination, $request = [], $parseJson 'error' => true, 'reason' => $e->getMessage() ]; - + } } -} + +} \ No newline at end of file