如何将数组传递给 PHP SoapClient 调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3780543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:04:27  来源:igfitidea点击:

How to pass an array into a PHP SoapClient call

phpsoap-client

提问by Colin

Using PHP and SoapClient.

使用 PHP 和 SoapClient。

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

我需要将以下XML传递到SOAP请求-即多重<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>

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:

以上不起作用,因为<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
        )
    )
);

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

但这会导致重复的密钥,因此只<stay>发送 's之一。

I'm running this as:

我运行这个:

$soapClient->saveReservation($xml);

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

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



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.

一些进一步的信息。上面的例子是超级简化的,所以这是我正在做的一个真实的使用例子,实现了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;

This fails, with the message object hasn't 'stay' propertywhich 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>

这将失败,并显示object hasn't 'stay' property由于相同问题而导致的消息,即<stays>标签应包含 1 个或多个<stay>标签。如果我设置$reservation->stays['stay'] = $stay;,然后它被接受,但也只让我有一个<stay><stays>

Additionally, the SOAP request looks like this:

此外,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>

回答by osondoar

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

'stay' 只需要定义一次。这应该是正确答案:

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

回答by benjy

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

假设当您实例化 时$soapClient,您是在 WSDL 模式下进行的,以下应该起作用:

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

回答by jivanrij

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

我也遇到了这个问题并找到了解决方案。Stays 需要是一个以 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

我在这个页面上找到了我的答案:https: //bugs.php.net/bug.php?id=45284

回答by sagesolutions

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.

我也遇到了这个问题,使用参数作为数组调用soap。我的数组必须从索引 0 开始才能工作。

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

回答by denormalizer

Try this:

尝试这个:

$xml = array(
  'stays' => array(
    'stay' => array(
      array( /* start end */ ),
      array( /* start end */ ),
      array( /* start end */ )
    )
  )
);