bencagri bencagri - 3 months ago 23
PHP Question

Php SOAP Client building request

I wanna build a request with php soap client.
The provider sent us a sample request.

here is my code;

$client = new SoapClient('http://myurl.com/TrevooWS.svc?wsdl' , array('trace' => true, 'exception' => false));

$auth_ns = 'http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Authentication.IO';
$base_ns = 'http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Base';

$object = new stdClass();
$form = new stdClass();
$form->Username = new SoapVar( 'USER' , XSD_STRING , null , null, 'trev1', $auth_ns);
$form->Password = new SoapVar( 'PASS' , XSD_STRING , null , null, 'trev1', $auth_ns);
$form->IsTestMode = new SoapVar( 1 , XSD_INTEGER, null , null, 'trev1', $auth_ns);
$form->ClientName = new SoapVar( 'CLIENT' , XSD_STRING , null , null, 'trev1', $auth_ns);
$form->ClientIP = new SoapVar( '0' , XSD_STRING , null , null, 'trev1', $auth_ns);

$object->request = new stdClass();
$object->request->Form = new SoapVar($form , SOAP_ENC_OBJECT, null , null, 'tem', $base_ns);

$object->request->Form->enc_namens = $auth_ns;
$object->request->Form->enc_name = 'trev1';
$object->request->enc_namens = $base_ns;
$object->request->enc_name = 'tem';

try{

$result = $client->Login( $object );

var_dump($result);

} catch (SoapFault $e){

echo $e;
var_dump($client->__getLastRequest());
var_dump($client->__getLastResponse());

}


Expected request from provider. They sent this sample request.

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:trev="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Base" xmlns:trev1="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Authentication.IO">
<soapenv:Header />
<soapenv:Body>
<tem:Login>
<tem:request>
<trev1:Form>
<trev1:ClientIP>0</trev1:ClientIP>
<trev1:ClientName>s</trev1:ClientName>
<trev1:IsTestMode>0</trev1:IsTestMode>
<trev1:Password>PASSWORD</trev1:Password>
<trev1:Username>USERNAME</trev1:Username>
</trev1:Form>
</tem:request>
</tem:Login>
</soapenv:Body>
</soapenv:Envelope>


What I get;

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.datacontract.org/2004/07/Trevoo.WS.Entities.Authentication.IO" xmlns:ns2="http://tempuri.org/">
<SOAP-ENV:Body>
<ns2:Login>
<ns2:request>
<ns1:Form>
<ns1:Username>USERNAME</ns1:Username>
<ns1:Password>PASSWORD</ns1:Password>
<ns1:IsTestMode>1</ns1:IsTestMode>
<ns1:ClientName>CLIENT</ns1:ClientName>
<ns1:ClientIP>0</ns1:ClientIP>
</ns1:Form>
</ns2:request>
</ns2:Login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>


This is response for Login method.

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
<faultstring xml:lang="tr-TR">String reference not set to an instance of a String.&#xD;
Parameter name: s</faultstring>
<detail>
<ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HelpLink i:nil="true" />
<InnerException i:nil="true" />
<Message>String reference not set to an instance of a String.&#xD;
Parameter name: s</Message>
<StackTrace>at System.Text.Encoding.GetBytes(String s)&#xD;
at Trevoo.Utilities.Crypto.MD5Hash(String input) in c:\Trevoo_TFS\*****\trunk\main\Trevoo\Projects\Core\Trevoo.Core\Utilities\Crypto.cs:line 131&#xD;
at Trevoo.Accounts.AuthController.ReadonlyLogin(LoginRequest request) in c:\Trevoo_TFS\*****\trunk\main\Trevoo\Projects\Core\Trevoo.Core\Accounts\AuthController.cs:line 22&#xD;
at Trevoo.Accounts.AuthController.Login(LoginRequest request) in c:\Trevoo_TFS\*****\trunk\main\Trevoo\Projects\Core\Trevoo.Core\Accounts\AuthController.cs:line 10&#xD;
at Trevoo.WS.Services.Base.Login(T_LoginRequest request) in c:\Trevoo_TFS\*****\trunk\main\Trevoo\Projects\WS\Trevoo.WS\Services\Authentication\Login.cs:line 13&#xD;
at SyncInvokeLogin(Object , Object[] , Object[] )&#xD;
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)&#xD;
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD;
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD;
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)&#xD;
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</StackTrace>
<Type>System.ArgumentNullException</Type>
</ExceptionDetail>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>


I couldnt see what is wrong. Could you help me?

Answer

you need to order elements as expected

Ordering must be;

->ClientIP
->ClientName
->IsTestMode
->Password
->Username