Php SOAP客户端构建请求

时间:2016-08-11 11:44:22

标签: php soap

我想用php soap客户端构建一个请求。 提供商向我们发送了样本请求。

这是我的代码;

$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());

}

来自提供商的预期请求。他们发送了此样本请求。

<?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>

我得到了什么;

<?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>

这是对Login方法的回复。

<?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>

我看不出有什么问题。你能帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您需要按预期订购元素

订购必须;

->ClientIP
->ClientName
->IsTestMode
->Password
->Username
相关问题