如何将数组传递到 PHP SoapClient 调用
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个:
Try this:
我还遇到了这个问题,以参数作为数组调用soap。我的数组必须从索引 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.
我遇到了类似的问题,我必须在这个结构中发布数据。
接受的答案对我不起作用
如果接受的答案对你不起作用
+对大家的小提示,也许它可能会对某人有所帮助,使用
$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
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');
通常我使用 osondoar 的答案,但有一次我不得不以这种方式更改数组结构。
请注意,它在结构的不同级别具有相同的名称
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.And then the corresponding part of the request turned out like this:
“stay”只需定义一次。这应该是正确的答案:
'stay' has to be defined just once. This should be the right answer:
假设当您实例化
$soapClient
时,您是在 WSDL 模式下进行的,则以下内容应该有效:Assuming that when you instantiated
$soapClient
, you did so in WSDL mode, the following should work:我也遇到了这个问题并找到了解决方案。 Stays 需要是一个以 0 开头的升序键的数组。
我在此页面上找到了答案: 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.
I found my answer on this page: https://bugs.php.net/bug.php?id=45284