Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I preview the XML that PHP SOAP wants to send before sending it?

Tags:

php

soap

As per the title, is it possible to output the XML that a new SoapClient has created before trying to run a __soapCall() to ensure it's correct before actually sending it to the SOAP server?

like image 674
bcmcfc Avatar asked Oct 26 '10 07:10

bcmcfc


People also ask

How can I tell if SOAP is enabled in PHP?

In PHP to check whether SOAP enabled or not use built in function class_exists() : var_dump(class_exists("SOAPClient")); It also could be user to check any of modules classes.

How do I send a SOAP request in HTML?

To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.

What is PHP SOAP Extension?

A PHP SOAP Extension can be used to provide and consume Web services. In other words, this PHP extension can be used by PHP developers to write their own Web Services, as well as to write clients to make use of the existing Web services.

What is the use of SOAP in PHP?

The soap:encodingStyle attribute determines the data types used in the file, but SOAP itself does not have a default encoding. soap:Envelope is mandatory, but the next element, soap:Header , is optional and usually contains information relevant to authentication and session handling.


1 Answers

You could use a derived class and overwrite the __doRequest() method of the SoapClient class.

<?php
//$clientClass = 'SoapClient';
$clientClass = 'DebugSoapClient';
$client = new $clientClass('http://www.webservicex.com/CurrencyConvertor.asmx?wsdl');
$client->sendRequest = false;
$client->printRequest = true;
$client->formatXML = true;

$res = $client->ConversionRate( array('FromCurrency'=>'USD', 'ToCurrency'=>'EUR') );
var_dump($res);

class DebugSoapClient extends SoapClient {
  public $sendRequest = true;
  public $printRequest = false;
  public $formatXML = false;

  public function __doRequest($request, $location, $action, $version, $one_way=0) {
    if ( $this->printRequest ) {
      if ( !$this->formatXML ) {
        $out = $request;
      }
      else {
        $doc = new DOMDocument;
        $doc->preserveWhiteSpace = false;
        $doc->loadxml($request);
        $doc->formatOutput = true;
        $out = $doc->savexml();
      }
      echo $out;
    }

    if ( $this->sendRequest ) {
      return parent::__doRequest($request, $location, $action, $version, $one_way);
    }
    else {
      return '';
    }
  }
}

prints

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webserviceX.NET/">
  <SOAP-ENV:Body>
    <ns1:ConversionRate>
      <ns1:FromCurrency>USD</ns1:FromCurrency>
      <ns1:ToCurrency>EUR</ns1:ToCurrency>
    </ns1:ConversionRate>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
NULL

But you'd have to change the actual code a bit for this to work which I try to avoid when possible (i.e. let tools do the work).

like image 108
VolkerK Avatar answered Sep 28 '22 01:09

VolkerK