views:

69

answers:

1

Using PHP and SoapClient.

I need to pass the following XML into a soap request - i.e. multiple <stay>'s within <stays>.

<reservation>
    <stays>
        <stay>
            <start_date>2011-01-01</start_date>
            <end_date>2011-01-15</end_date>
        </stay>
        <stay>
            <start_date>2011-01-16</start_date>
            <end_date>2011-01-30</end_date>
        </stay>
    </stays>
</reservation>

The problem is that I'm passing the data in as an array:

$xml = array('reservation' => array(
    'stays' => array(
        array(
            'start_date' => '2011-01-01',
            'end_date'   => 2011-01-15
        ),
        array(
            'start_date' => '2011-01-16',
            'end_date'   => 2011-01-30
        )
    )
);

The above doesn't work, as <stay> is not defined. So the alternative is:

$xml = array('reservation' => array(
    'stays' => array(
        'stay' => array(
            'start_date' => '2011-01-01',
            'end_date'   => 2011-01-15
        ),
        'stay' => array(
            'start_date' => '2011-01-01',
            'end_date'   => 2011-01-15
        )
    )
);

But that results in duplicate keys, so only one of the <stay>'s is sent.

I'm running this as:

$soapClient->saveReservation($xml);

Any ideas on how I can structure the array so that the above XML is generated?


Some further information. The above examples were super-simplified, so here's a real use example of what I'm doing, with benjy's suggestion implemented.

$options = $this->api->getDefaultOptions();
$options['baseProductCode'] = '123'.$basket->accommodation['feed_primary_identifier'];
#                             ^^^^^ just to ensure it fails and doesn't process
$reservation = new stdClass();

$reservation->external_id = $order->pb_ref;
$reservation->etab_id = $basket->accommodation['feed_primary_identifier'];
$reservation->reservation_type = 'gin';
$reservation->firstname = $order->forename;
$reservation->lastname  = $order->surname;
$reservation->birthdate = date('Y-m-d', strtotime('- 21 YEAR'));
$reservation->stays = array();
$details = $basket->getDetailedBasketContents();
foreach ($details['room_types'] as $roomTypeId => $roomType) {
  foreach($roomType['instances'] as $instance) {
    $stay = new stdClass();
    $stay->nb_rooms = 1;
    $stay->room_type_code = $roomTypeId;
    $stay->start_date = date('Y-m-d', strtotime($order['checkin']));
    $stay->end_date   = date('Y-m-d', strtotime($order['checkout']));
    $stay->occupants  = array();
    foreach($instance['occupancy']['occupants'] as $key => $occupantData) {
      if ($occupantData['forename'] and $occupantData['surname']) {
        $occupant = new stdClass();
        $occupant->firstname = $occupantData['forename'];
        $occupant->lastname  = $occupantData['surname'];
        $occupant->pos = 100;
        $occupant->birthdate = date('Y-m-d', strtotime('- 21 YEAR'));
        $stay->occupants[] = $occupant;
      }
    }
    $reservation->stays[] = $stay;
  }
}

$options['reservation'] = new stdClass();
$options['reservation']->reservation = $reservation;


//echo XmlUtil::formatXmlString($this->api->);

try {
  $this->parsePlaceOrderResponse($this->api->__soapCall('saveDistribReservation2', $options));
} catch (Exception $e) {
  echo $e->getMessage();
  echo XmlUtil::formatXmlString($this->api->__getLastRequest());
  echo XmlUtil::formatXmlString($this->api->__getLastResponse());
}
exit;

This fails, with the message object hasn't 'stay' property which is due to the same issue, that the <stays> tag should contain 1 or more <stay> tags. If I set $reservation->stays['stay'] = $stay; then it is accepted, but that again only allows me to have a single <stay> within <stays>

Additionally, the SOAP request looks like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hom="homingwns" xmlns:v1="...">
   <soapenv:Header/>
   <soapenv:Body>
      <hom:saveDistribReservation2>
         <base_id>?</base_id>
         <username>?</username>
         <password>?</password>
         <partnerCode>?</partnerCode>
         <baseProductCode>?</baseProductCode>
         <reservation>
            <v1:reservation>
               <v1:external_id>?</v1:external_id>
               <v1:etab_id>?</v1:etab_id>
               <v1:reservation_type>?</v1:reservation_type>
               <!--Optional:-->
               <v1:option_date>?</v1:option_date>
               <!--Optional:-->
               <v1:gender>?</v1:gender>
               <!--Optional:-->
               <v1:firstname>?</v1:firstname>
               <v1:lastname>?</v1:lastname>
               <!--Optional:-->
               <v1:birthdate>?</v1:birthdate>
               <!--Optional:-->
               <v1:stays>
                  <v1:stay>
                     <v1:nb_rooms>?</v1:nb_rooms>
                     <v1:room_type_code>?</v1:room_type_code>
                     <v1:start_date>?</v1:start_date>
                     <v1:end_date>?</v1:end_date>
                     <!--Optional:-->
                     <v1:occupants>
                        <!--Optional:-->
                        <v1:occupant>
                           <!--Optional:-->
                           <v1:gender>?</v1:gender>
                           <!--Optional:-->
                           <v1:firstname>?</v1:firstname>
                           <v1:lastname>?</v1:lastname>
                           <!--Optional:-->
                           <v1:birthdate>?</v1:birthdate>
                           <v1:pos>?</v1:pos>
                        </v1:occupant>
                     </v1:occupants>
                  </v1:stay>
               </v1:stays>
            </v1:reservation>
         </reservation>
      </hom:saveDistribReservation2>
   </soapenv:Body>
</soapenv:Envelope>
A: 

Assuming that when you instantiated $soapClient, you did so in WSDL mode, the following should work:

$stay1 = new stdClass();
$stay1->start_date = "2011-01-01";
$stay1->end_date = "2011-01-15";
$stay2 = new stdClass();
$stay2->start_date = "2011-01-01";
$stay2->end_date = "2011-01-15";
$stays = array();
$stays[0] = $stay1;
$stays[1] = $stay2;
$soapClient->saveReservation(
    array("reservation" => array("stays" => $stays))
);
benjy
Thanks. I'll give that a go - helpfully the database on the endpoint is down at the moment so I can't test it, but I will do ASAP.
Colin
I don't see how this will get the multiple <stay> tags in there though, but am assuming you know something I don't - will give it a go once I can.
Colin
Re: you don't see how this will work - trust me, no one else does. PHP's SOAP client sucks.
benjy
Any luck on this?
benjy
Sorry, weekend got in the way - I'm picking this up now and will be back shortly with the results.
Colin
Not had much luck with that, as far as I can tell for the same reasons. I've updated the question with the actual code I'm running (the original was a very simplified example) so hopefully this might be of use. Thanks for suggestions so far.
Colin