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>';
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.