-
-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added XML support #7
base: craft3
Are you sure you want to change the base?
Conversation
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.
} elseif ($format == 'xml') { | ||
$xmlbody = simplexml_load_string($response->getBody(), null, LIBXML_NOCDATA); | ||
|
||
$json = json_encode($xmlbody); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The $json variable is unnecessary here. Put json_encode($xmlbody)
on the next line instead where $json
is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, had no idea you replied.
I tried you suggestion and the body
key had a string of the JSON object returned. So the XML needs encoding to JSON then decoding.
I suppose it could go $body = json_decode(json_encode($xmlbody), true);
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think that snippet is what Patrik is suggesting. A bit DRYer I guess, but I personally prefer it as you have it for readability... (If I'm misunderstanding your point @patrikalienus feel free to correct me)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeh, it was more for readability. It was a bit rushed too at the time, so wasn't thinking to save a few lines. I guess where there isn't much in it, then @patrikalienus suggestion is still valid.
I wonder if Symfony's XmlEncoder may be a better solution here than If I'm not mistaken it's already included in Craft, and the implementation would look something like this: use Symfony\Component\Serializer\Encoder\XmlEncoder;
...
$xmlEncoder = new XmlEncoder();
$body = $xmlEncoder->decode($response->getBody(), 'xml'); @jamiematrix let me know what you think... |
I'll give it a go @jalendport thanks. I blame Google in a way :) The change was a quick search for a project, |
I got |
Default set to JSON, but there's now support if the endpoint is returning XML.
Currently XML attributes are not returned (other than from the parent node), but that's next on the list.