0

Im building a SOAP client that consumes from a SOAP web services in a Drupal 8 module.

I need to generate this final XML:

$inpunt_data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:qual="http://websoap.com/">

                           <soapenv:Header/>

                           <soapenv:Body>

                              <qual:myMethod>

                                 <!--Optional:-->

                                 <qual:xmlMethod><![CDATA[

                                <Tag>
                                </Tag>
                               </qual:Method>';

How do I generate soapenv headers?

I'm using simplexml:

$getXML = new SimpleXMLElement($input_data);  
             $urlTarifa = 'http://soapurl.com?wsdl';
             $client = new SoapClient($urlTarifa);  
1

1 Answer 1

2

This is how I've done it:

some-feed.html.twig

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <atom:link href="{{ some_variable_set_in_preprocess }}" rel="self" type="application/rss+xml"/>
      /* -- rest of feed omitted */
  </channel>
</rss>

Then hook_theme() to register the template:

function hook_theme() {
  return [
    'some_feed' => [
      'render element' => 'elements',
      'template' => 'some-feed',
    ],
  ];
}

You can then generate a response with that theme:

$build = [
  '#theme' => 'some_feed',
];

$response = new \Symfony\Component\HttpFoundation\Response();
$xml = \Drupal::service('renderer')->renderRoot($build);
$response->setContent($xml);
$response->headers->set('Content-Type', 'text/xml');

You now have a Response object to work with. Depending on where you are working, you can return that, or set it as a response.

1
  • Im doing this on a custom module, inside a controller, then, i send the variables to the twig...i will try! Commented Feb 5, 2020 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.