2014-02-24 136 views
0

我使用的代碼SOAP體產生問題

try { 
    //set up the service client using WSDL 
    echo "Connecting to server using WSDL<br />"; 

    $client = new SoapClient("http://demo-hotelws.touricoholidays.com/hotelflow.svc?wsdl", array("trace" => true, "exceptions" => true, 'soap_version' => SOAP_1_1)); 

    //var_dump($client->__getFunctions()); 
    //var_dump($client->__getTypes()); 

    $headerbody = array(
         'LoginName' => 'vibXXX', 
         'Password' => '111111', 
         'Culture' => 'en_US', 
         'Version' => '7.123' 
        ); 

    //$x = new SoapVar($x, SOAP_ENC_OBJECT, \"AuthenticationHeader","http://www.itworks.nl/"); 
    $header=new SoapHeader('http://tourico.com/webservices/','AuthenticationHeader', $headerbody); 
    $client->__setSoapHeaders(array($header)); 
    echo "SoapHeaders set sucessfully<br />"; 


    //$roominfo = array('AdultsNum' => 1,'ChildNum' => 1,'ChildAges' => 8); 
    $parameter = array(
     'Destination' => 'NYC', 
     'HotelCityName' => '', 
     'HotelLocationName' => '', 
     'HotelName' => '', 
     'CheckIn' => '2014-02-15', 
     'CheckOut' => '2014-02-17', 
     'RoomsInformation' => array('RoomInfo' => array('AdultNum' => 1, 'ChildNum' => 1, 'ChildAges' => array('ChildAge' => 8))), 
     'MaxPrice' => 0, 
     'StarLevel' => 0, 
     'AvailableOnly' => 1, 
     'PropertyType' => 'NotSet', 
     'ExactDestination' => true 
    ); 


    $result = $client->SearchHotels($parameter); 


    echo("<br />REQUEST :<br />" . htmlspecialchars($client->__getLastRequest()) . "<br/>"); 
    echo("<br />RESPONSE:<br />" .htmlspecialchars($client->__getLastResponse()) . "<br />"); 
} 
catch (SoapFault $ex) 
{ 
    echo "Error:<br />" . nl2br($ex->faultcode) . '<br /><br />Error Details:<br />'. nl2br($ex->faultstring) . '<br />'; 
    echo("<br />REQUEST :<br />" . htmlspecialchars($client->__getLastRequest()) . "<br/>"); 
    echo("<br />RESPONSE:<br />" .htmlspecialchars($client->__getLastResponse()) . "<br />");  
} 

我想請求被生成按照下面,

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:aut="http://schemas.tourico.com/webservices/authentication" xmlns:hot="http://tourico.com/webservices/hotelv3" xmlns:hot1="http://schemas.tourico.com/webservices/hotelv3"> 
    <soapenv:Header> 
     <aut:AuthenticationHeader> 
     <aut:LoginName>vibXXX</aut:LoginName> 
     <aut:Password>111111</aut:Password> 
     <!--Optional:--> 
     <aut:Culture>en_US</aut:Culture> 
     <!--Optional:--> 
     <aut:Version>7.123</aut:Version> 
     </aut:AuthenticationHeader> 
    </soapenv:Header> 
    <soapenv:Body> 
     <hot:SearchHotels> 
     <!--Optional:--> 
     <hot:request> 
      <!--Optional:--> 
      <hot1:Destination>NYC</hot1:Destination> 
      <!--Optional:--> 
      <hot1:HotelCityName>New York</hot1:HotelCityName> 
      <!--Optional:--> 
      <hot1:HotelLocationName></hot1:HotelLocationName> 
      <!--Optional:--> 
      <hot1:HotelName></hot1:HotelName> 
      <hot1:CheckIn>2014-02-15</hot1:CheckIn> 
      <hot1:CheckOut>2014-02-18</hot1:CheckOut> 
      <!--Optional:--> 
      <hot1:RoomsInformation> 
       <!--Zero or more repetitions:--> 
       <hot1:RoomInfo> 
        <hot1:AdultNum>2</hot1:AdultNum> 
        <hot1:ChildNum>0</hot1:ChildNum> 
        <!--Optional:--> 
        <hot1:ChildAges> 
        <!--Zero or more repetitions:--> 
        <hot1:ChildAge age="0"/> 
        </hot1:ChildAges> 
       </hot1:RoomInfo> 
      </hot1:RoomsInformation> 
      <hot1:MaxPrice>0</hot1:MaxPrice> 
      <hot1:StarLevel>0</hot1:StarLevel> 
      <hot1:AvailableOnly>1</hot1:AvailableOnly> 
      <hot1:PropertyType>NotSet</hot1:PropertyType> 
      <hot1:ExactDestination>1</hot1:ExactDestination> 
     </hot:request> 
     </hot:SearchHotels> 
    </soapenv:Body> 
</soapenv:Envelope> 

但是,它不產生適當的。我生成的XML是,

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tourico.com/webservices/hotelv3" xmlns:ns2="http://tourico.com/webservices/"> 
    <SOAP-ENV:Header> 
    <ns2:AuthenticationHeader> 
     <item> 
     <key>LoginName</key> 
     <value>vibXXX</value> 
     </item> 
     <item> 
     <key>Password</key> 
     <value>111111</value> 
     </item> 
     <item> 
     <key>Culture</key> 
     <value>en_US</value> 
     </item> 
     <item> 
     <key>Version</key> 
     <value>7.123</value> 
     </item> 
    </ns2:AuthenticationHeader> 
    </SOAP-ENV:Header> 
    <SOAP-ENV:Body> 
    <ns1:SearchHotels/> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

任何人都可以告訴我什麼是我的代碼問題?

回答

0

我已經改變了我的代碼現在它的工作正常。

try { 
    class AuthHeader { 
     var $LoginName; 
     var $Password; 
     var $Culture; 
     var $Version; 

     function __construct($loginInfo) { 
      $this->LoginName = $loginInfo['LoginName']; 
      $this->Password = $loginInfo['Password']; 
      $this->Culture = $loginInfo['Culture']; 
      $this->Version = $loginInfo['Version']; 
     } 
    } 

    // set current soap header with login info 
    $client = new SoapClient("http://demo-hotelws.touricoholidays.com/hotelflow.svc?wsdl", array('trace' => TRUE)); 

    // create header params array 
    $headerParams = array('LoginName' => 'vibXXX', 
          'Password' => '111111', 
          'Culture'  => 'en_US', 
          'Version'  => '7.123'); 

    // create AuthHeader object 
    $auth = new AuthHeader($headerParams); 

    // Turn auth header into a SOAP Header 
    $header = new SoapHeader('http://schemas.tourico.com/webservices/authentication', 'AuthenticationHeader', $auth, false); 

    // set the header 
    $client->__setSoapHeaders($header); 

    // Create the shipping request 
    $d = new stdClass; 
    $d->ChildAge = '8'; 

    $b = new stdClass; 
    $b->AdultNum = '2'; 
    $b->ChildNum = '0'; 
    //$b->ChildAges = array($d); 

    $p = new stdClass; 
    $p->request->Destination   = 'NYC'; 
    $p->request->HotelCityName   = ''; 
    $p->request->HotelLocationName  = ''; 
    $p->request->HotelName    = ''; 
    $p->request->CheckIn    = '2014-02-27'; 
    $p->request->CheckOut    = '2014-03-02'; 
    $p->request->RoomsInformation  = array($b); 
    $p->request->MaxPrice    = 0; 
    $p->request->StarLevel    = 0; 
    $p->request->AvailableOnly   = 1; 
    $p->request->PropertyType   = 'NotSet'; 
    $p->request->ExactDestination  = true; 

    $quote = $client->SearchHotels($p); 

    echo("<br />REQUEST :<br />" . htmlspecialchars($client->__getLastRequest()) . "<br/>"); 
    echo("<br />RESPONSE:<br />" .htmlspecialchars($client->__getLastResponse()) . "<br />"); 
} 
catch (SoapFault $ex) 
{ 
    echo "Error:<br />" . nl2br($ex->faultcode) . '<br /><br />Error Details:<br />'. nl2br($ex->faultstring) . '<br />'; 
    echo("<br />REQUEST :<br />" . htmlspecialchars($client->__getLastRequest()) . "<br/>"); 
    echo("<br />RESPONSE:<br />" .htmlspecialchars($client->__getLastResponse()) . "<br />");  
} 

但有一個問題,我怎麼能傳遞childAge在我的請求?

我參考從How to create PHP SOAP request with multiple namespaces

+0

要通過childAge需要聲明與屬性「年齡」爲一個新的類並創建childAges標籤元素中的新對象。 ''childAges'=>新的childAge($ age)'。將$ age值賦給類構造函數中的對象屬性。 –