Get received XML from PHP SOAP Server

后端 未结 2 1397

I\'m using the build-in SOAP-Server within a symfony2 application and beside handling the StdClass-Object, I would need to read the complete xml received for debugging and l

2条回答
  •  生来不讨喜
    2021-01-04 20:59

    The raw XML transmitted in a SOAP envelope should be in the POST body. In a Symfony application, you can get the body of the POST request by creating a Request object and calling its getContents() method.

    Within a Controller

    You can get request contents easily in the controller, like so:

    // src/MyProject/MyBundle/Controller/MyController.php
    use Symfony\Component\HttpFoundation\Request;
    
    ...
    
    $request = Request::createFromGlobals();
    $soapEnvelope = $request->getContents();
    

    Within a Service

    Best practice (for Symfony 2.4+) is to inject a RequestStack into your service class within the service container. You can do it as a constructor argument to your service class, by invoking a setter method, etc. Here's a quick example using injection via the constructor.

    In your service container:

    // src/MyProject/MyBundle/Resources/config/services.xml
    
        
    
    

    Then in your service class:

    // src/MyProject/MyBundle/Service/MyService.php
    use Symfony\Component\HttpFoundation\RequestStack;
    
    ....
    
    class MyServiceClass
    {
        /**
         * @var RequestStack $rs
         */
        private $requestStack;
    
        /**
         * Constructor
         *
         * @param RequestStack $requestStack
         */
        public function __construct(RequestStack $requestStack)
        {
            $this->requestStack = $requestStack;
        }
    
        /**
         * Some method where you need to access the raw SOAP xml
         */
        public function myMethod()
        {
            $request = $this->requestStack->getCurrentRequest();
            $soapEnvelope = $request->getContents();
        }
    }
    

    Reference documentation:

    http://symfony.com/blog/new-in-symfony-2-4-the-request-stack

提交回复
热议问题