4

I'm New to webservices i'm trying to call webservice with soapClient() and it's generating Request XML which is not in expected format

Below is expected format of request XML

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <ns2:EndUserRequest xmlns:ns2="http://www.example.net/EndUserRequest">
        <ns2:companyCode>MD</ns2:companyCode>
        <ns2:customerBranch>60</ns2:customerBranch>
        <ns2:customerNumber>112946</ns2:customerNumber>
        <ns2:endUserName>Some Name</ns2:endUserName>
        <ns2:ContactName />
        <ns2:address />
        <ns2:city />
        <ns2:state />
        <ns2:postalCode />
        <ns2:email />
        <ns2:phoneNumber />
        <ns2:countryCode>US</ns2:countryCode>
    </ns2:EndUserRequest>
</soapenv:Body>
</soapenv:Envelope>

Below is XML Request generating by my code

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.emaple.com/EndUserRequest">
    <SOAP-ENV:Body>
        <ns1:EndUserRequest xmlns:ns1="http://www.example.net/EndUserRequest">
            <companyCode>MD</companyCode>
            <customerBranchNumber>360</customerBranchNumber>
            <customerNumber>53494711</customerNumber>
            <endUserName>ABCED</endUserName>
            <ContactName></ContactName>
            <address></address>
            <city></city>
            <state></state>
            <postalCode></postalCode>
            <email></email>
            <phoneNumber></phoneNumber>
            <countryCode>US</countryCode>
        </ns1:EndUserRequest>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Below is my code

$client = new SoapClient('https://api-beta.example.com:443/enduser/v1/enduserlist?wsdl', array(
        "trace" => 1,                
        "stream_context" => stream_context_create($streamContext),
        'cache_wsdl' => WSDL_CACHE_NONE
        ));

        $endUserRequest = new stdClass;
        $endUserRequest->companyCode = 'MD';
        $endUserRequest->customerBranchNumber = '560';
        $endUserRequest->customerNumber = '59471321';
        $endUserRequest->endUserName = 'Somename';
        $endUserRequest->ContactName = '';
        $endUserRequest->address = '';
        $endUserRequest->city = '';
        $endUserRequest->state = '';
        $endUserRequest->postalCode = '';
        $endUserRequest->email = '';
        $endUserRequest->phoneNumber = '';
        $endUserRequest->countryCode = 'US';



    $requestSoapVar = new SoapVar($endUserRequest, SOAP_ENC_OBJECT,null,null,'EndUserRequest','http://www.example.com/EndUserRequest');
        $res  = $client->GetEndUsers($requestSoapVar);
        echo '<textarea style="width:600px;height:500px">';
            echo "\n-------Request Header------\n";
            echo $client->__getLastRequestHeaders();
            echo "\n-------Request------\n";
            echo $client->__getLastRequest();
            echo "\n-------Response Header------\n";
            echo $client->__getLastResponseHeaders();
            echo "\n-------Response------\n";
            echo $client->__getLastResponse();
        echo '</textarea>';

        echo '<textarea style="width:600px;height:500px">';
        print_r($res);
        echo '</textarea>';
Kathir Selvan
  • 43
  • 1
  • 4

1 Answers1

3

Every member of your object must be a SoapVar object because there is a namespace for them. Just encode your object as the follwing example shows.

$oEndUserRequest = new StdClass();
$oEndUserRequest->companyCode = new SoapVar(
    'MD', 
    XSD_STRING, 
    null, 
    null,
    'companyCode',
    'http://www.example.com/EndUserRequest'
);

Just do it for everey class member and you 'll get the expected result.

For advanced reason here 's an example how to change the prefix of the namespace. You have to know, that the PHP SoapClient object nor the SoapVar object have a way to manually set a namespace prefix. In a normal case it is unnecessary to set a prefix for a namespace.

The PHP SoapClient object has a __doRequest method, in which you can edit the XML. You have to Code your own SoapClient extending the PHP SoapClient.

class MySoapClient extends SoapClient {
    public function __doRequest($sRequest, $sLocation, $sAction, $iVersion, $iOneWay = 0) {
        $sRequest = str_replace('ns1', 'ns2', $sRequest);
        $this->__last_request = $sRequest;

        return parent::__doRequest(ltrim($sRequest), $sLocation, $sAction, $iVersion, $iOneWay);
    }
}

In my eyes it is not neccessary to change the namepace prefix. If so, just use the __doRequest method for your purpose.

Marcel
  • 4,854
  • 1
  • 14
  • 24
  • You have to add 'ns2' like this `XSD_STRING, null, 'ns2', 'companyCode'` to get the namespace to be `ns2` no ? – Bobot Nov 29 '16 at 14:10
  • 3
    Nope, the 'ns2' or 'ns1' declarations are automatically done by the soap client. The third and fourth parameter of the SoapVar object are for type declarations. The fifth and sixth parameters are for the node name and node namespace. So the namespace ist done with the last parameter. It is not important how a namespace is prefixed in the xml. The prefix 'ns1' works fine, because there is no other namespace declared apart from the soapenv namespace. – Marcel Nov 29 '16 at 14:17
  • @marcel : Now i'm seeing "ns1" for all params in my XML request , but "ns2" is expected – Kathir Selvan Nov 29 '16 at 14:17
  • I 've edited the answer with an example, how you can change the prefix of the namespace manually. Be careful with the use of __doRequest. ;) – Marcel Nov 29 '16 at 14:28
  • What about the response? How does it look like? I 've edited my answer. Please recognize $this->__last_request class member of MySoapClient. – Marcel Nov 30 '16 at 07:19