Get received XML from PHP SOAP Server

后端 未结 2 1398

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:39

    I was looking for the same thing and finally found it. Hope this helps you or someone else.

    $postdata = file_get_contents("php://input");
    

    The $postdata variable will have the raw XML. Found through the following two links:

    http://php.net/manual/en/reserved.variables.httprawpostdata.php

    http://php.net/manual/en/soapserver.soapserver.php

    0 讨论(0)
  • 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
    <service id="my.service" class="MyServiceClass">
        <argument type="service" id="request_stack" />
    </service>
    

    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

    0 讨论(0)
提交回复
热议问题