2014-11-22 91 views
1

我使用SOAP客戶端&其工作正常消費的Web服務。我的SOAP客戶端的代碼是:如何獲得只在SOAP客戶端輸出XML PHP

$wsdl = 'http://www.example.com/erpsync/erp_sync.asmx?WSDL'; 

$trace = true; 
$exceptions = false; 

$xml_array['userName'] = 'Admin'; 
$xml_array['password'] = '[email protected]'; 
$xml_array['ItemNo'] = '4005002335910'; 

try 
{ 
    $client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions)); 
    //$response = $client->GetAllItemData($xml_array); 
    $response = $client->GetAllItemStock($xml_array); 
    //$response = $client->GetStockByItem($xml_array); 

    //echo $client->__getLastRequest(); 

} 

catch (Exception $e) 
{ 
    echo "Error!"; 
    echo $e -> getMessage(); 
    echo 'Last response: '. $client->__getLastResponse(); 
} 

var_dump($response); 

輸出看起來是這樣的:

object(stdClass)#2 (1) { 
    ["GetAllItemStockResult"]=> 
    object(stdClass)#3 (1) { 
    ["any"]=> 
    string(113098) "<ERPitems xmlns=""><ItemData><ItemCode>AG40-01811-I354 .....</ERPitems>" 
} 
} 

在輸出中,我想消除<ERPitems xmlns="">標記之前和</ERPitems>標籤後的一切。

怎麼辦?

+0

輸出字符串的那一部分?我很努力地看到它與SOAP本身有什麼關係(除非你打算爲自動分析xml-in-xml(討厭那些服務)的'GetAllItemStockResult'響應定義一個自定義類)。 – Wrikken 2014-11-22 17:26:45

+0

我沒有定義任何自定義類。我想弄清楚,從那裏這個數據來自(前標籤) – 2014-11-22 17:41:52

+0

...從你的'var_dump',其中轉儲您收到的目標...你可能想要做一些與'$響應 - > GetAllItemStockResult-> any'。 – Wrikken 2014-11-22 17:44:55

回答

0

使用的preg_match找到模式

$str = '<ERPitems xmlns=""><ItemData><ItemCode>AG40-01811-I354 .....</ERPitems>'; 

echo getTextBetweenTags($str, "ERPitems"); 

function getTextBetweenTags($string, $tagname) 
{ 
    $pattern = "/<$tagname.*?>(.*?)<\/$tagname>/"; 
    preg_match($pattern, $string, $matches); 
    return $matches[1]; 
} 
+0

非常感謝。這很有幫助 – 2014-11-26 03:36:53

+0

如果這是您正在尋找的答案,請將其標記爲已回答。 – Demodave 2014-11-26 14:20:01