Пишем xml-строку в логи monolog без пробелов между xml-узлами
Если мы хотим записать xml-строку в логи с помощью пакета monolog, то строка запишется в одну строку с пробелами между xml-узлов.
Пример:
$xmlString = <<<EOF <?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> </catalog> EOF; ; $xml = simplexml_load_string($xmlString); dlog( sprintf('xml response: "%s"', trim($xml->asXML())) );
В логах будет запись:
[2018-02-17 18:26:45] app.DEBUG: xml response: "<?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> </catalog>" [] []
Запись не выглядит единым целым, что мешает чтению логов (по крайней мере мне).
Желаемого можно достичь с помощью \DOMDocument.
Пример:
$xmlString = <<<EOF <?xml version="1.0"?> <catalog> <book id="bk101"> <author>Gambardella, Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> <book id="bk102"> <author>Ralls, Kim</author> <title>Midnight Rain</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-12-16</publish_date> <description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description> </book> </catalog> EOF; ; $xml = simplexml_load_string($xmlString); dlog( sprintf('Xml-ответ после закупки товаров из 1С: "%s"', trim( call_user_func( function (\SimpleXMLElement $xml) { $dom = new \DOMDocument('1.0'); $dom->preserveWhiteSpace = false; $dom->loadXML($xml->asXML()); return $dom->saveXML(); }, $xml ) )) );
В логах уже будет приятная запись:
[2018-02-17 18:26:45] app.DEBUG: xml response: "<?xml version="1.0"?> <catalog><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre><price>44.95</price><publish_date>2000-10-01</publish_date><description>An in-depth look at creating applications with XML.</description></book><book id="bk102"><author>Ralls, Kim</author><title>Midnight Rain</title><genre>Fantasy</genre><price>5.95</price><publish_date>2000-12-16</publish_date><description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description></book></catalog>" [] []
Примечание: в заметке опущена реализация функции dlog() и предполагается, что к вам прилетает xml в виде \SimpleXMLElement.