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

发布于 2024-09-24 15:48:52 字数 6447 浏览 5 评论 0原文

使用 PHP 和 SoapClient。

我需要将以下 XML 传递到肥皂请求中 - 即 内的多个

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

上面的方法不起作用,因为 未定义。因此,另一种选择是:

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

但这会导致重复的密钥,因此仅发送其中一个

我运行此命令:

$soapClient->saveReservation($xml);

关于如何构造数组以便生成上述 XML 有什么想法吗?


一些进一步的信息。上面的例子是超级简化的,所以这是我正在做的一个真实的使用例子,并实施了本杰的建议。

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

此操作失败,并显示消息 object has not 'stay' property ,这是由于相同的问题,即 标记应包含 1 个或多个 标签。如果我设置 $reservation->stays['stay'] = $stay; 那么它会被接受,但这又只允许我有一个 > 在

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

巾帼英雄 2024-10-01 15:48:53

试试这个:

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

Try this:

$xml = array(
  'stays' => array(
    'stay' => array(
      array( /* start end */ ),
      array( /* start end */ ),
      array( /* start end */ )
    )
  )
);
一身软味 2024-10-01 15:48:53

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

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

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'],
  ],
]);
十雾 2024-10-01 15:48:53

我遇到了类似的问题,我必须在这个结构中发布数据。
接受的答案对我不起作用

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

如果接受的答案对你不起作用

+对大家的小提示,也许它可能会对某人有所帮助,使用 $xml = ['键' => 'val']; 而不是 $xml = array('key' => 'val');

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

随遇而安 2024-10-01 15:48:53

通常我使用 osondoar 的答案,但有一次我不得不以这种方式更改数组结构。

请注意,它在结构的不同级别具有相同的名称option

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

然后请求的相应部分结果是这样的:

<option>
    <option>
        <name>First option</name>
    </option>
    <option>
        <name>Second option</name>
    </option>
</option>

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>
绻影浮沉 2024-10-01 15:48:52

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

'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
                    )
              )  
    )
));
一片旧的回忆 2024-10-01 15:48:52

假设当您实例化 $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))
);

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))
);
泪冰清 2024-10-01 15:48:52

我也遇到了这个问题并找到了解决方案。 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);

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文