SOAP体生成问题

时间:2014-02-24 12:54:04

标签: php xml soap soap-client soapheader

我使用了代码

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>

有人能告诉我我的代码有什么问题吗?

1 个答案:

答案 0 :(得分: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

获得了参考