我从不同的 SOAP 方法获取数据。举个例子:
$wsdl = 'the_url_of_wsdl';
$client = new SoapClient($wsdl);
$parameters = array(all_the_parameters_inside);
$values = $client->the_method($parameters);
$xml = $values->the_methodResult->any;
$sxml = simplexml_load_string($xml);
$wsdl2 = 'the_url2_of_wsdl';
$client2 = new SoapClient($wsdl2);
$parameters2 = array(all_the_parameters_inside);
$values2 = $client->the_method2($parameters2);
$xml2 = $values2->the_method2Result->any;
$sxml2 = simplexml_load_string($xml2);
$wsdl3 = 'the_url3_of_wsdl';
$client3 = new SoapClient($wsdl3);
$parameters3 = array(all_the_parameters_inside);
$values3 = $client->the_method3($parameters3);
$xml3 = $values3->the_method3Result->any;
$sxml3 = simplexml_load_string($xml3);
如果我 print_r $sxml、$sxml2 和 $sxml3,在里面我可以获得任何数据,比如说
echo $sxml->name;
、echo $sxml2->id;
和 echo $sxml3->description;
。
棘手的部分是这些值来自不同的方法,所以我如何将它们组合在同一 HTML 结构的循环中,就像这样:
<div class="article">
<div class="name">the value from the first method</div>
<div class="id">the value from the second method</div>
<div class="description">the value from the third method</div>
</div>
<div class="article">
<div class="name">the value from the first method</div>
<div class="id">the value from the second method</div>
<div class="description">the value from the third method</div>
</div>
<div class="article">
<div class="name">the value from the first method</div>
<div class="id">the value from the second method</div>
<div class="description">the value from the third method</div>
</div>
编辑: 这是我想要实现的扩展示例。两种方法的示例。
第一个soap方法中的print_r(sxml)显示如下结构:
SimpleXMLElement Object
(
[NewDataSet] => SimpleXMLElement Object
(
[HotelFacility] => Array
(
[0] => SimpleXMLElement Object
(
[HotelId] => 1
[HotelCode] => TEST
[FacNo] => 6
[FacName] => test (bank)
[OriginalName] =>
[IsPriced] => No
[MediaID] => 1
[Note] => SimpleXMLElement Object
(
)
[UseinWebFilter] => true
)
[1] => SimpleXMLElement Object
(
[HotelId] => 1
[HotelCode] => TEST2
[FacNo] => 12
[FacName] => test center
[OriginalName] => Test Center
[IsPriced] => No
[MediaID] => 1
[Note] => SimpleXMLElement Object
(
)
[UseinWebFilter] => true
) //and the list continues...
第二个soap方法中的print_r(sxml2)显示如下结构:
SimpleXMLElement Object
(
[NewDataSet] => SimpleXMLElement Object
(
[HotelPresentation] => Array
(
[0] => SimpleXMLElement Object
(
[RecId] => 5
[HotelId] => 14
[HotelCode] => test hotel
[PresText] => "the description".
[TextType] => HTML
[TextCategory] => GENERAL
[MediaID] => 4
[CrtDate] => 2016-01-06T15:34:00+02:00
[ChgDate] => 2016-01-06T15:34:00+02:00
)
[1] => SimpleXMLElement Object
(
[RecId] => 3
[HotelId] => 4
[HotelCode] => test hotel2
[PresText] => //and the list continues....
如何合并这两个数组并得到结果,假设我想制作一个像这样的 html 结构:
<div class="article">
<div class="name">the value facName from the first method</div>
<div class="id">the value hotelCode from the second method</div>
</div>
<div class="article">
<div class="name">the value facName from the first method</div>
<div class="id">the value hotelCode from the second method</div>
</div>
<div class="article">
<div class="name">the value facName from the first method</div>
<div class="id">the value hotelCode from the second method</div>
</div>
HTML 文章将根据 SOAP 方法中的结果数量动态显示。
最佳答案
怎么样:
$sxml1 = simplexml_load_string($xml1);
$sxml2 = simplexml_load_string($xml2);
$facilities = $sxml1->NewDataSet->HotelFacility;
$presentations = $sxml2->NewDataSet->HotelPresentation;
foreach ($facilities as $i => $facility) {
echo '<div class="article">';
echo '<div class="name">'. $facility->FacName .'</div>';
echo '<div class="id">'. $presentations[$i]->HotelCode .'</div>';
echo '</div>';
}
当然,假设 HotelFacility 和 HotelPresentation 数组元素可以像这样链接在一起(通过它们的索引位置)。根据你的解释,很难确定两者之间的关系。
关于php - 组合来自不同 SOAP 方法的循环值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418407/