18

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>
Colin
  • 784
  • 2
  • 8
  • 21
  • Why not just serialize the 'stays' array and assign the contents to the xml element . On the receiving side, you can unserialize it easily. – bcosca Sep 23 '10 at 17:13
  • I don't have control over the soap endpoint. It has to be XML according to the wsdl. But this is a fairly standard XML structure so there must be a way to represent it. – Colin Sep 23 '10 at 22:46
  • See also http://stackoverflow.com/questions/577795/passing-array-to-soap-function-in-php – Kevin Vaughan Sep 24 '10 at 00:45
  • Looking at the link now Kevin - thanks. It does appear to be related, though when putting it into practise it doesn't appear to help - will keep fiddling. – Colin Sep 27 '10 at 12:47
  • Try `$reservation->stays['stay'][] = $stay;` – benjy Sep 27 '10 at 14:26
  • I've reverted to the pure array format, and am alternately running it through the array_to_objecttree() function in the other question. I'm currently using that method (or to be precise - `$reservation['stays']['stay'][] = $stay;`) but this still results in the error `SOAP-ERROR: Encoding: object hasn't 'nb_rooms' property`. If I change that back to `$reservation['stays']['stay'] = $stay;` all is fine, but that's back to being limited to only one stay. – Colin Sep 27 '10 at 14:34

7 Answers7

26

'stay' has to be defined just once. This should be the right answer:

$xml = array('reservation' => array(
'stays' => array(
    'stay' => array(
                    array(
                          'start_date' => '2011-01-01',
                          'end_date'   => 2011-01-15
                    ),
                    array(
                          'start_date' => '2011-01-01',
                          'end_date'   => 2011-01-15
                    )
              )  
    )
));
rafarr
  • 454
  • 5
  • 12
  • Thanks, but this question is 2 1/2 years old, and I'm not at the job, let alone the same project, so I'm not able to test/verify. Thanks for contributing though. – Colin Apr 13 '13 at 13:41
  • 3
    Seeing as you answered three years after the question, it seems appropriate that I accept it a further three years on :p - Looks like incutonez verified the solution. – Colin May 06 '16 at 13:36
  • Thanks for this. Everywhere else I've looked basically says you can't do it with an array because of the duplicate keys. This is a very simple solution. – Matt K Sep 27 '16 at 19:18
5

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
  • 4,664
  • 7
  • 38
  • 43
  • 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 Sep 24 '10 at 08:58
  • I don't see how this will get the multiple tags in there though, but am assuming you know something I don't - will give it a go once I can. – Colin Sep 24 '10 at 08:59
  • 3
    Re: you don't see how this will work - trust me, no one else does. PHP's SOAP client sucks. – benjy Sep 24 '10 at 16:10
  • Sorry, weekend got in the way - I'm picking this up now and will be back shortly with the results. – Colin Sep 27 '10 at 11:31
  • 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 Sep 27 '10 at 12:01
1

I also had this problem and found the solution. Stays needs to be an array with ascending keys starting with 0.

$client = new SoapClient('http://myservice.com?wsdl');
$stays[] = array('startDate'=>'01-01-2013', 'endDate'=>'02-02-2013');
$stays[] = array('startDate'=>'02-02-2013', 'endDate'=>'03-03-2013');
$params = array(
  'reservation' => array('stays'=>$stays)
);
$client->saveReservation($params);

I found my answer on this page: https://bugs.php.net/bug.php?id=45284

jivanrij
  • 157
  • 2
  • 12
0

I also ran into this issue calling soap with an parameter as an array. My array has to start with index 0 for it to work.

$client->__soapCall(my_function_name, [
  'body' => [
    'date' => '201930', 
    'ids' => [0 => '32001', 1 => '32002'],
  ],
]);
sagesolutions
  • 491
  • 4
  • 9
0

Try this:

$xml = array(
  'stays' => array(
    'stay' => array(
      array( /* start end */ ),
      array( /* start end */ ),
      array( /* start end */ )
    )
  )
);
denormalizer
  • 2,186
  • 3
  • 24
  • 36
0

I had similar problem and I had to post data in this structure. Accepted answer didn't work for me

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

maybe it might help somebody if accepted answear doesn't work for you

+ minor tip for all of you, use $xml = ['key' => 'val']; instead of $xml = array('key' => 'val');

Tonoslav
  • 517
  • 2
  • 6
  • 20
0

Usually I use osondoar's answer, but once I had to change the array structure this way.

Note that it has the same name option at different levels of the structure.

$xml_data = [
    'option' => [
        [
            'option' => [
                'name' => 'First option',
            ],
        ],
        [
            'option' => [
                'name' => 'Second option',
            ],
        ],
    ],
];

And then the corresponding part of the request turned out like this:

<option>
    <option>
        <name>First option</name>
    </option>
    <option>
        <name>Second option</name>
    </option>
</option>
Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68