mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-03-14 12:23:33 +00:00
Strip namespaces from namespaced XML plugin response, so we get usuable output
This commit is contained in:
parent
0aa38428f6
commit
afc29e15d5
2 changed files with 50 additions and 2 deletions
|
|
@ -18,7 +18,7 @@ class XmlResponseParser implements ResponseParser
|
|||
}
|
||||
|
||||
try {
|
||||
$xml = simplexml_load_string($response->body());
|
||||
$xml = $this->simplexml_load_string_strip_namespaces($response->body());
|
||||
if ($xml === false) {
|
||||
throw new Exception('Invalid XML content');
|
||||
}
|
||||
|
|
@ -43,4 +43,25 @@ class XmlResponseParser implements ResponseParser
|
|||
|
||||
return $array;
|
||||
}
|
||||
|
||||
function simplexml_load_string_strip_namespaces($xml_response) {
|
||||
$xml = simplexml_load_string($xml_response);
|
||||
if ($xml === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$namespaces = array_keys($xml->getDocNamespaces(true));
|
||||
$namespaces = array_filter($namespaces, function($name) { return !empty($name); });
|
||||
if (count($namespaces) == 0) {
|
||||
return $xml;
|
||||
}
|
||||
$namespaces = array_map(function($ns) { return "$ns:"; }, $namespaces);
|
||||
|
||||
$xml_no_namespaces = str_replace(
|
||||
array_merge(["xmlns="], $namespaces),
|
||||
array_merge(["ns="], array_fill(0, count($namespaces), '')),
|
||||
$xml_response
|
||||
);
|
||||
return simplexml_load_string($xml_no_namespaces);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue