2015-09-26 15 views
0

Я пытаюсь получить значение из ответа SOAP с помощью PHP. Независимо от того, что я сделал, я не мог получить значение в переменной. Пожалуйста помоги.Как получить значение из ответа SOAP с помощью PHP?

Я использую WordPress's wp_remote_post(), чтобы отправить форму и получить ответ.

$response = wp_remote_post($url, $args); 
$xml = $response['body']; 

Вот ответ в SOAP:

<soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> 
    <soap:body> 
     <sendtransactionsactionresponse xmlns="http://tempuri.org/"> 
      <sendtransactionsactionresult>113</sendtransactionsactionresult> 
     </sendtransactionsactionresponse> 
    </soap:body> 
</soap:envelope> 

Вот то, что я уже пробовал:

// Din't work 
$value = $xml->body->sendtransactionsactionresponse->sendtransactionsactionresult; 

// Din't work 
$value = $xml['body']['sendtransactionsactionresponse']['sendtransactionsactionresult']; 

//Returned an empty Object 
simplexml_load_string($xml); 

Пытались несколько вещей, но ни один из них не работал. Мне нужно получить значение sendtransactionsactionresult в переменной для сравнения. Пожалуйста помоги.

Благодаря

EDIT
var-dump из $response.

array(5) { ["headers"]=> array(8) { ["connection"]=> string(5) "close" ["date"]=> string(29) "Sat, 26 Sep 2015 18:12:23 GMT" ["server"]=> string(17) "Microsoft-IIS/6.0" ["x-powered-by"]=> string(7) "ASP.NET" ["x-aspnet-version"]=> string(9) "4.0.30319" ["cache-control"]=> string(18) "private, max-age=0" ["content-type"]=> string(35) "application/soap+xml; charset=utf-8" ["content-length"]=> string(3) "401" } ["body"]=> string(401) " 
<soap:envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> 
<soap:body> 
<sendtransactionsactionresponse xmlns="http://tempuri.org/"> 
<sendtransactionsactionresult>113</sendtransactionsactionresult> 
</sendtransactionsactionresponse> 
</soap:body> 
</soap:envelope> 
" ["response"]=> array(2) { ["code"]=> int(200) ["message"]=> string(2) "OK" } ["cookies"]=> array(0) { } ["filename"]=> NULL } 
+0

Вы используете SoapClient из PHP? –

+0

Нет. Я использую 'wp_remote_post()' для отправки формы и получения ответа. Подобно этому '$ response = wp_remote_post ($ url, $ args); $ xml = $ response ['body']; ' – Abhik

+0

Вы обновите свой вопрос, чтобы включить эту важную деталь (: –

ответ

0

Вот один из способов:

$foo = new SimpleXMLElement($xmlstr); 
$bar = json_decode(json_encode($foo)); 
print_r($bar); 

Я уверен, что вы можете выяснить все остальное.

0

Вам просто нужно использовать правильные методы, чтобы получить правильный объект из строки XML, здесь вы идете:

$response_body = wp_remote_retrieve_body($response); 
$xml = simplexml_load_string($response_body); 
$value = $xml->body->sendtransactionsactionresponse->sendtransactionsactionresult; 
1

Согласно WordPress wp_remote_post() функции документации результат ответа HTTP будет в массиве. К вашим var_dump данным, body ключ существует как допустимая строка XML.

Вам нужно только очистить XML от мыла: приставки

$response = wp_remote_post('http://69.94.141.22/SaveTransactions.asmx', $args); 

if(is_wp_error($response)) 
    return $response->get_error_message(); 

$xml = str_replace('soap:', '', $response['body']); 

$obj = simplexml_load_string($xml); 

$result = $obj->body->sendtransactionsactionresponse->sendtransactionsactionresult; 

print_r($result); 

Я попробовал код, и он отлично работает! https://eval.in/440149

Смежные вопросы