【问题标题】:Response in SOAP XML to Array conversion in PHPSOAP XML 中的响应到 PHP 中的数组转换
【发布时间】:2016-05-20 14:14:48
【问题描述】:

我有如下所示的 SOAP XML 响应,请建议我如何从中找到房间代码、酒店代码、货币等数据。 我也使用了下面的代码,但它不工作意味着它返回空。

$xmlObject = simplexml_load_string($xmlString);

$array = json_decode(json_encode($xmlObject), true);

对应的 XML 响应:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <ResponseId xmlns="http://www.example.com/">e96c9439-6049-4abb-b070-f7f0024153b7@52-20-187-176</ResponseId>
  </soap:Header>
  <soap:Body>
    <ns4:FetchRoomAllotmentResponse xmlns:ns4="http://www.example.com/" xmlns:ns2="http://www.example.com" xmlns:ns3="http://www.example.com/">
      <ns4:FetchRoomAllotmentResult>
        <ns4:Response>
          <ns4:Hotel Code="201511191642109768" Name="TestCMHotel" />
          <ns4:Rooms>
            <ns4:Room Code="171578" Name="2 Bed Super Deluxe">
              <ns4:Rates>
                <ns4:Rate Currency="INR" Description="2 Bed Super Deluxe" Id="71624608833328" MealPlan="MAP" ValidFrom="2015-11-19" ValidTo="2017-04-30">
                  <ns4:Inclusions>
                    <ns4:Inclusion code="test inclusion test inclusion test inclusion test inclusion test inclusion">test inclusion test inclusion test inclusion test inclusion test inclusion</ns4:Inclusion>
                  </ns4:Inclusions>
                </ns4:Rate>
              </ns4:Rates>
            </ns4:Room>
            <ns4:Room Code="19505" Name="AC Deluxe Room AC">
              <ns4:Rates>
                <ns4:Rate Currency="INR" Description="AC Deluxe Room AC" Id="5835231260301" MealPlan="EP" ValidFrom="2015-11-19" ValidTo="2017-05-31">
                  <ns4:Inclusions>
                    <ns4:Inclusion code="test 1">test 1</ns4:Inclusion>
                    <ns4:Inclusion code="test inclusion test inclusion test inclusion test inclusion test inclusion">test inclusion test inclusion test inclusion test inclusion test inclusion</ns4:Inclusion>
                  </ns4:Inclusions>
                </ns4:Rate>
              </ns4:Rates>
            </ns4:Room>
            <ns4:Room Code="18303" Name="Standar AC">
              <ns4:Rates>
                <ns4:Rate Currency="INR" Description="Standar AC" Id="79045408874412" MealPlan="CP" ValidFrom="2015-11-19" ValidTo="2017-06-30">
                  <ns4:Inclusions>
                    <ns4:Inclusion code="test wifi and swiimming pool on a stay og one night or multiple nights 1 night or multiple nights te">test wifi and swiimming pool on a stay og one night or multiple nights 1 night or multiple nights te</ns4:Inclusion>
                  </ns4:Inclusions>
                </ns4:Rate>
              </ns4:Rates>
            </ns4:Room>
          </ns4:Rooms>
        </ns4:Response>
      </ns4:FetchRoomAllotmentResult>
    </ns4:FetchRoomAllotmentResponse>
  </soap:Body>
</soap:Envelope>

【问题讨论】:

    标签: php xml web-services soap


    【解决方案1】:

    试试这个例子soap 对数组值的响应得到:

    if( ! $response = curl_exec($cURL)){<br/>
      trigger_error(curl_error($cURL));<br/>
    }
    
    curl_close ( $cURL );<br/><br/>
    $xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);<br/>
    $xml = simplexml_load_string($xml);<br/>
    $json = json_encode($xml);<br/>
    $responseArray = json_decode($json,true);
    
    //Find value 
    findKey($responseArray['sBody'],'val');
    

    此函数将帮助从数组中查找键和值

    public static function findKey($array, $keySearch)
    {
        foreach ($array as $key => $item) {
            if ($key === $keySearch) {
                return $item;
            }
            else {
                if (is_array($item)) {
                    return  self::findKey($item, $keySearch);
                }
            }
        }
        return false;
    }
    

    【讨论】:

      【解决方案2】:

      为此使用simplexml_load_string()

      将一串 XML 解释为一个对象

      更多信息在这里 http://php.net/manual/en/function.simplexml-load-string.php

      【讨论】:

      • 谢谢ecorvo,我已经尝试过这个功能,但它返回空对象
      【解决方案3】:

      也许这对你有用。

      $xml = new SimpleXMLElement($xmlObject);
      $xml->registerXPathNamespace('ns4', 'http://www.example.com/');
      foreach($xml->xpath('//ns4:Room') as $room) {
          //var_export($room);
          //var_export($room->xpath("//ns4:Inclusion"));
          echo "ROOM Code" . $room["Code"]."<br/>";
          foreach($room->xpath("//ns4:Inclusion") as $inclusion){
              echo "Inclusion Code" .$inclusion["code"]."<br/>";
          }
      }
      

      您可以在此处阅读更多信息Parse XML with Namespace using SimpleXML

      【讨论】:

        【解决方案4】:

        朋友们,我得到了答案。非常感谢您的 cmets。

        $parser = simplexml_load_string($result);  
        $parserEnv = $parser->children('soap', true);
        $hotel = $parserEnv->Body->children('ns4', true)
            ->FetchRoomAllotmentResponse->children('ns4', true)
            ->FetchRoomAllotmentResult->children('ns4', true)
            ->Response->children('ns4', true)->Hotel[0]->attributes();
        
           echo 'Hotel Code= '.$hotel['Code']. '<br>';
           echo 'Hotel Name= '.$hotel['Name']. '<br>';
        
        $room = $parserEnv->Body->children('ns4', true)
            ->FetchRoomAllotmentResponse->children('ns4', true)
            ->FetchRoomAllotmentResult->children('ns4', true)
            ->Response->children('ns4', true)->Rooms->children('ns4', true);
        
           for($i=0; $i<count($room); $i++)
           {
             $r=$room->Room[$i]->attributes();
             echo 'Room'.$i.' Code='.$r['Code']. '<br>';
             echo 'Room'.$i.' Name='.$r['Name']. '<br>';
            $rate=$room->Room[$i]->children('ns4', true)->Rates->children('ns4', true)->Rate[0]->attributes();
            echo 'Currency= '.$rate['Currency']. '<br>';
           echo 'Description= '.$rate['Description']. '<br>';
           echo 'Id= '.$rate['Id']. '<br>';
           echo 'MealPlan= '.$rate['MealPlan']. '<br>';
           echo 'ValidFrom= '.$rate['ValidFrom']. '<br>';
           echo 'ValidTo= '.$rate['ValidTo']. '<br>';
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-31
          • 1970-01-01
          • 2017-06-24
          • 1970-01-01
          • 1970-01-01
          • 2017-05-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多