2011-11-21 29 views
4

我已經花了幾個小時與SOAPClient,仍然沒有結果,希望有人能幫助我。我試圖創建和發送到網絡服務下面的代碼:如何爲SOAP客戶端函數創建適當的xml?

POST /PortalServices/PortalServices.asmx HTTP/1.1 
Host:***.***.com 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://***.com/CreateUser" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Header> 
    <UserCredentials xmlns="http://***.com/"> 
     <UserName>string</UserName> 
     <PassWord>string</PassWord> 
     <ServiceName>string</ServiceName> 
    </UserCredentials> 
    </soap:Header> 
    <soap:Body> 
    <CreateUser xmlns="http://***.com/"> 
     <user> 
     <Name>string</Name> 
     <Email>string</Email> 
     <PassWord>string</PassWord> 
     <WsServices> 
      <WsService> 
      <Id>int</Id> 
      </WsService> 
     </WsServices> 
     </user> 
    </CreateUser> 
    </soap:Body> 
</soap:Envelope> 

爲了得到這個XML我使用下面的代碼:

<?php 

class WsService { 
public $Id = "****"; 
} 

class WsServices { 
public $WsService; 
public function __construct() { 
     $this->WsService = new WsService(); 
     } 
} 


class WsUser { 

public $Name = 'Sasha Lavasov'; 
public $Email = '[email protected]'; 
public $PassWord = 'test'; 
public $WsServices; 

public function __construct() { 
     $this->WsServices = new WsService(); 
     } 
} 

    $ini = ini_set("soap.wsdl_cache_enabled","0"); 

    $soap_url = 'http://***.***.com/PortalServices/PortalServices.asmx?wsdl'; 
    $soapClient = new \SoapClient($soap_url, array(
     'soap_version'=>SOAP_1_2, 
     'trace' => 1, 
     'exceptions' => true, 
     'cache_wsdl' => WSDL_CACHE_NONE 
     )); 

    $hdd = new stdClass(); 
    $hdd->UserName = '***'; 
    $hdd->PassWord = '***'; 
    $hdd->ServiceName = '***'; 

    $hd = new SoapVar($hdd, SOAP_ENC_OBJECT); 


     $headers = new \SoapHeader('http://***.***.com/PortalServices', 'UserCredentials', $hd); 

     // Prepare Soap Client 
     $soapClient->__setSoapHeaders(array($headers)); 

     $user = new WsUser(); 
     // Setup the CreateUser parameters 
     $ap_param = array(
     'Name' => 'Sasha Lavasov', 
     'WsUser' => $user 
        ); 


     $param = new \SoapParam($user, 'WsUser'); 
     // Call CreateUser() 
     $error = 0; 

     try { 
     //Build user entity 
     $usr = new WsUser(); 
     $wsServices = new StdClass(); 

     $id = new StdClass(); 
     $id->Id = '***'; 
     $wsServices->WsService = $id; 
     $usr->WsServices = $wsServices; 

     $createUser = new stdClass(); 
     $createUser->user = $usr; 

     $createUser = new SoapVar($createUser, SOAP_ENC_OBJECT, "WsUser", "http://***.***.com/PortalServices/PortalServices.asmx?op=CreateUser"); 

     $pm = new SoapParam($createUser, "user"); 

     $soapClient->CreateUser($pm); 
      //Here I get xml 
      var_dump($soapClient->__getLastRequest()); die(); 
     } catch (\SoapFault $fault) { 
      $error = 1; 
      print($fault->faultcode." - ".$fault->faultstring); die(); 
     } 

     if ($error == 0) { 
     //    DO SOMETHING 
      } else { 

      } 

     $res = $soapClient->__getLastResponse(); 
     print_r($res); die(); 

而是我得到這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://***.***.com/PortalServices/PortalServices.asmx?op=CreateUser" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://***.com/" xmlns:ns3="http://***.***.com/PortalServices"> 
<env:Header> 

<ns3:UserCredentials> 
<UserName>***</UserName> 
<PassWord>***</PassWord> 
<ServiceName>***</ServiceName> 
</ns3:UserCredentials></env:Header> 

<env:Body> 
<ns2:CreateUser xsi:type="ns1:WsUser"> 
<user> 
<Name>Sasha Lavasov</Name> 
<Email>[email protected]</Email> 
<PassWord>test</PassWord> 
<WsServices><WsService><Id>***</Id></WsService></WsServices> 
</user> 
</ns2:CreateUser> 
</env:Body> 
</env:Envelope> 

並得到FALSE結果。 在服務器的功能列表(使用__getFunctions()),我有以下幾點:

CreateUserResponse CreateUser(CreateUser $parameters) 

誰能幫助我或者只是分不清哪裏是我錯了?

+1

如果您進行詳細比較您認爲它的SOAP XML以及您不喜歡的可憐的PHP SoapClient(),唯一的區別是兩個名稱空間ns1和ns3,這兩個名稱空間在您的PHP代碼中另外指定。 –

+0

@sasha你可以發佈WSDL嗎? Tnx –

+0

問題是我使用了錯誤的命名空間,並且是的,WSDL工作。但我使用CURL和正確的xml重寫了它。這足夠了,因爲我只收到1個布爾響應,並且很容易解析。 謝謝,夥計們! –

回答

1

只是一個建議。

在你 XML您已經:

<UserCredentials xmlns="http://***.com/"> 

,但在你的PHP設置:

$headers = new SoapHeader('http://***.***.com/PortalServices', 'UserCredentials', $hd); 

,那麼你已經2個不同的命名空間:HTTP:// ** *。***。com/PortalServices而不是http://***.com/