1

I have big log file in xml format that I need to store in a single database field as an array.. I get that file by

$request->getContent()

and whatever syntax I write I still get xml format..

This is my code.

public function test(Request $request)
{
    file_put_contents('/var/www/html/var/logs/ehi.log', print_r($request->getContent() . '',true), FILE_APPEND);

    $array = json_decode(json_encode((array)($request->getContent())),true);

    dump($array);die;
}

and the content look like..

enter image description here

developsss
  • 51
  • 1
  • 8
  • show an example of the xml content – Tschallacka Aug 22 '18 at 08:35
  • I updated the post.. @Tschallacka – developsss Aug 22 '18 at 08:40
  • 1
    *I have big log file in xml format that I need to store in a single database field as an array*. Why? Why would you want this? There's some pretty good reasons logs are usually saved to files instead of databases. – Loek Aug 22 '18 at 08:42
  • What data type does `$request->getContent()` return? It looks like you're just casting a string to an array and then converting it to and from JSON for... some reason? Also, database fields don't generally contain "arrays" - you can store JSON in them fine, but if you're just storing a string then why not just store the XML as-is? – iainn Aug 22 '18 at 08:42
  • @developsss you might want to look at https://stackoverflow.com/questions/15892639/converting-soap-xml-response-to-a-php-object-or-array – Tschallacka Aug 22 '18 at 08:44
  • If you really want to do it this way - you need `simplexml_load_string($request->getContent())` and pass this into `json_encode()` – Nigel Ren Aug 22 '18 at 08:48

1 Answers1

1

Inspiration from: converting SOAP XML response to a PHP object or array and How do I convert an object to an array?

First you need to get the Object you want from the SOAP response. You do that by converting it to a simplexml object with simplexml_load_string

You then "navigate" the simplexml object by going down the tree of children or using xpath. Whatever your preferrence is.

When you have the object you can then convert it to an array by using the function listed in the second question(get_object_vars) to convert it to an array.

$soap     = simplexml_load_string($request->getContent());
$transaction = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')
                     ->Body->children()
                         ->GetTransaction;
$transaction_as_array = get_object_vars($transaction);
Tschallacka
  • 27,901
  • 14
  • 88
  • 133