2013-05-15 3 views
0

Я получаю доступ к SOAP-серверу через CURL (его единственный способ подключения PHP). Это ответ, который я получаю:Parse CURL SOAP response

<s:envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"> 
<s:header> 
    <a:action s:mustunderstand="1">PublicApi/IPropertyService/CreatePropertyResponse</a:action> 
</s:header> 
<s:body> 
    <createpropertyresponse xmlns="PublicApi"> 
     <createpropertyresult xmlns:b="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi.Property" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
      <message xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">Successfully completed the operation</message> 
      <result xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">0</result> 
      <transactiondate xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">2013-05-15T04:07:48.6565312Z</transactiondate> 
      <b:propertyid>55</b:propertyid> 
     </createpropertyresult> 
    </createpropertyresponse> 
</s:body> 

Я пытаюсь вытащить содержание:

<message xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">Successfully completed the operation</message> 
<result xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">0</result> 
<b:propertyid>55</b:propertyid> 

Но я не могу понять, как разобрать его. Я пробовал «simplexml_load_string», который выдает кучу ошибок, таких как «предупреждение пространства имен: xmlns: URI PublicApi не является абсолютным»

Любые предложения?

+0

Вы пробовали использовать стандартную клиентскую библиотеку мыла? ie Zend Soap Client http://framework.zend.com/manual/2.0/en/modules/zend.soap.client.html Примечание: вам не нужно использовать весь MVC, только эту библиотеку для мыльных дисков –

+0

Если формат всегда почти такой же, preg_match() сделает это за вас. – Floris

+0

Также есть ли мыльный сервер, отправляющий действительные ответы? Вы можете проверить с помощью SoapUI (скачать бесплатно) http://www.soapui.org –

ответ

2

Немного взломать, но если формат ответа всегда примерно такой же, следующий может работать для вас:

?php 
// the message would come from somewhere else; I hard code it to test the expression that follows: 
$msg='<s:envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"> 
<s:header> 
    <a:action s:mustunderstand="1">PublicApi/IPropertyService/CreatePropertyResponse</a:action> 
</s:header> 
<s:body> 
    <createpropertyresponse xmlns="PublicApi"> 
     <createpropertyresult xmlns:b="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi.Property" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
      <message xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">Successfully completed the operation</message> 
      <result xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">0</result> 
      <transactiondate xmlns="http://schemas.datacontract.org/2004/07/EfxFramework.PublicApi">2013-05-15T04:07:48.6565312Z</transactiondate> 
      <b:propertyid>55</b:propertyid> 
     </createpropertyresult> 
    </createpropertyresponse> 
</s:body>'; 

// here comes the actual parsing: 
$reg1='/<message [^>]*>([^<]*)</'; 
$reg2='/<result [^>]*>([^<]*)</'; 
preg_match($reg1, $msg, $m); 
print "message: ". $m[1]."\n"; 
preg_match($reg2, $msg, $m); 
print "result: ".$m[1]."\n"; 
?> 

Результат выше:

message: Successfully completed the operation 
result: 0 

Пояснение :

[^>]*>: «любое количество символов, которые не >, а затем >

([^<]*): «захватить» все символы, которые НЕ <. вернуть их в $m[1]

Надеюсь, это поможет.