Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get received XML from PHP SOAP Server

Tags:

php

soap

symfony

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 logging. Is there a way to simply catch the transferred xml? It should be somewhere in the request header, but I simply can't find it there.

like image 542
Sgoettschkes Avatar asked Aug 01 '11 07:08

Sgoettschkes


2 Answers

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

like image 194
Steve Lindsey Avatar answered Sep 19 '22 15:09

Steve Lindsey


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

like image 44
Leo Bedrosian Avatar answered Sep 18 '22 15:09

Leo Bedrosian