2012-01-14 60 views
-2

我無法找到相同的問題,因此我決定打開新的。 我堅持下面的問題。 目標是使用soap消息在UI上顯示xml屬性值。 PHP代碼如下所示:無法解析來自PHP的XML

$client = new SoapClient("https://domain.com/xml/listener.asmx?WSDL"); 
$results = $client->ProductDescription(array('Username' => "anyuser", 'Password' => "anypassword", 'code' => "1108324")); 
print_r($results); 

結果我獲得以下消息

stdClass Object ([ProductDescriptionResult] => stdClass Object ([any] => Imagehttp://catalog2.elkogroup.com/pictures/prDesc/large/1108324_425_0_425NULL.jpgDescription<h1 class="tagline"><span style="font-size: small">Quality surround audio for music, movies and games</span></h1> <p>&nbsp;Sound Blaster 5.1 VX is the absolute choice for those looking for better quality audio solutions basic motherboard audio can not deliver.</p>Vendor Homepagehttp://en.europe.creative.com/products/productarchive.asp?category=1&subcategory=873&product=17510&nav=Description2CREATIVE 5.1 VX (SB1071) OEMAudio-InInput/Output connectors1Audio-OutInput/Output connectors1MicrophoneInput/Output connectors1Included AccessoriesQuick start Guide; User Guide (on CD); Installation CDUnit Brutto Volumecubm0.001555Unit Net Weightkg0.13Unit Gross Weightkg0.157)) 

的XML使用同樣的請求,我從SOAP UI獲得具有不同結構

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <ProductDescriptionResponse xmlns="https://ecom.elko.lv/xml"> 
     <ProductDescriptionResult> 
      <NewDataSet xmlns=""> 
       <Product> 
        <Criteria>Image</Criteria> 
        <Value>http://test/</Value> 
       </Product> 
       ... 
       ... 
      </NewDataSet> 
     </ProductDescriptionResult> 
     </ProductDescriptionResponse> 
    </soap:Body> 
</soap:Envelope> 

我試着使用不同方法讀取標準和值參數值,例如

一個)

foreach($Envelope->Body->ProductDescriptionResponse->ProductDescriptionResult->NewDataSet->Product as $p) 
     { 
      echo $p->Criteria."<br>"; 
      echo $p->Value."<br>"; 
     } 

錯誤消息是 「未定義變量:信封...」

B)

foreach ($results->xpath('//soap:Envelope[1]/soap:Body[1]/ProductDescriptionResponse[1]/ProductDescriptionResult[1]/NewDataSet[1]/Product/*') as $item) 
     { print_r($item);} 

錯誤消息是電話未定義的方法stdClass的::的xpath()

c) 還試圖註冊命名空間

$xml = simplexml_load_string($results); 
$xml->registerXPathNamespace('envoy', 'https://ecom.elko.lv/xml'); 

...

,但它說我simplexml_load_string()預計參數1是字符串

等等

能否請您幫忙弄清楚?

回答

2

把它從響應元素(ProductDescriptionResult),這是什麼SOAPClient會給你:

echo $results->ProductDescriptionResult->NewDataSet->Product[1]->Criteria; 

請記住,您不必XML在這裏了,在SOAPClient它轉換成一個漂亮的樹已經有了對象。可能在任何時候,上述路徑都不存在(我剛剛根據XML猜測它),在這種情況下,只需從get_object_vars中執行var_dump即可查看當前節點中的內容。哦,並且不要在瀏覽器中查看HTML輸出,查看源代碼,如果您查看HTML代碼,則調試輸出中的很多<>可能會「隱藏」內容。


編輯:這個作品在這裏

<?php 
class S extends SoapClient { 
     function __doRequest(){ 
       return <<<XML 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
     <ProductDescriptionResponse xmlns="https://ecom.elko.lv/xml"> 
     <ProductDescriptionResult> 
      <NewDataSet xmlns=""> 
       <Product> 
        <Criteria>Image1</Criteria> 
        <Value>http://test/1</Value> 
       </Product> 
       <Product> 
        <Criteria>Image2</Criteria> 
        <Value>http://test/2</Value> 
       </Product> 
      </NewDataSet> 
     </ProductDescriptionResult> 
     </ProductDescriptionResponse> 
    </soap:Body> 
</soap:Envelope> 
XML; 
     } 
} 
$d = new S(null,array('uri' => 'localhost','location' => 'localhost')); 
$result = $d->somerequest(); 
echo $result->NewDataSet->Product[0]->Criteria; 
+0

Thnaks你非常快速響應。我嘗試了你的建議,但我仍然不能閱讀Criteria論證的價值。錯誤消息是未定義的屬性:stdClass :: $ NewDataSet。我也檢查源,如你所建議的那裏,我可以找到XML結構。麥克風內置 user1149594 2012-01-14 20:43:14

+0

我在編輯它,但請...讓'var_dump'你的朋友,它會告訴你哪些變量是和他們有什麼。 – Wrikken 2012-01-14 20:55:07

+0

var_dump返回下面的結果。對象(stdClass的)[44] 公共 'ProductDescriptionResult'=> 對象(stdClass的)[45] 公衆 '任意'=>字符串「圖片 HTTP://catalog2.elkogroup .com/pictures/prDesc/large/1124919_425_0_425NULL.jpg user1149594 2012-01-14 21:12:55