如何将数组传递到 PHP SoapClient 调用中

2022-08-30 23:14:47

使用 PHP 和 SoapClient。

我需要将以下XML传递到肥皂请求中 - 即.<stay><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>

问题是我以数组的形式传入数据:

$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
        )
    )
);

上述方法不起作用,因为未定义。因此,替代方案是:<stay>

$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
        )
    )
);

但这会导致密钥重复,因此只发送其中一个 。<stay>

我运行这个作为:

$soapClient->saveReservation($xml);

关于如何构建数组以便生成上述XML的任何想法?


一些进一步的信息。上面的例子是超级简化的,所以这里有一个我正在做什么的真实使用例子,实现了benjy的建议。

$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;

此操作失败,并显示由于同一问题导致的消息,即标记应包含 1 个或多个标记。如果我设置,那么它被接受,但这再次只允许我有一个单一的object hasn't 'stay' property<stays><stay>$reservation->stays['stay'] = $stay;<stay><stays>

此外,SOAP 请求如下所示:

<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>

答案 1

“停留”必须只定义一次。这应该是正确的答案:

$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
                    )
              )  
    )
));

答案 2

假设在实例化 时,您是在 WSDL 模式下实例化的,那么以下操作应该可以正常工作:$soapClient

$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))
);

推荐