Get received XML from PHP SOAP Server
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 the开发者_如何转开发re a way to simply catch the transferred xml? It should be somewhere in the request header, but I simply can't find it there.
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
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
精彩评论