使用PHP和SoapClient。
我需要将以下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>
问题在于我将数据作为数组传递:
$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;
由于同一问题导致消息object hasn't 'stay' property
失败,<stays>
标记应包含1个或多个<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>
答案 0 :(得分:19)
'逗留'必须只定义一次。这应该是正确的答案:
$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
)
)
)
));
答案 1 :(得分:3)
假设您在实例化$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))
);
答案 2 :(得分:0)
试试这个:
$xml = array(
'stays' => array(
'stay' => array(
array( /* start end */ ),
array( /* start end */ ),
array( /* start end */ )
)
)
);
答案 3 :(得分: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);
我在此页面上找到了答案:https://bugs.php.net/bug.php?id=45284
答案 4 :(得分:0)
我也遇到了这个问题,用参数作为数组调用soap。我的数组必须从索引0开始才能工作。
* def data =
"""
{
qa: {
sos: 'https://sos.qa.tceu.net',
acn: 'https://acn.qa.tceu.net'
}
}
"""
* def env = 'qa'
* def urls = data[env]
* def action = 'sos'
* def actionUrl = urls[action]
* match actionUrl == 'https://sos.qa.tceu.net'
答案 5 :(得分:0)
我有类似的问题,我不得不在这种结构中发布数据。 接受的答案对我不起作用
$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
)
)
));
如果已接受的answear对您不起作用,也许会对某人有帮助
+给大家的小提示,使用 $xml = ['key' => 'val'];
代替 $xml = array('key' => 'val');