我正在尝试使用PHP从SOAP响应中获得一个值。不管我做了什么,我都无法得到变量中的值。请帮帮忙。
我正在使用WordPress的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的值来进行比较。请帮帮忙。
谢谢
编辑
var-dump of $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 } 发布于 2015-09-26 21:33:23
根据WordPress 员额()函数文档,http响应结果将在数组中。通过对您的var_dump数据,主体键作为有效的XML存在。
您只需从soap:前缀中清除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
发布于 2015-09-26 17:10:57
有一种方法:
$foo = new SimpleXMLElement($xmlstr);
$bar = json_decode(json_encode($foo));
print_r($bar);我相信你能搞清楚剩下的。
发布于 2015-09-26 17:17:49
您只需要使用适当的方法从XML字符串中检索有效的对象,如下所示:
$response_body = wp_remote_retrieve_body($response);
$xml = simplexml_load_string($response_body);
$value = $xml->body->sendtransactionsactionresponse->sendtransactionsactionresult;https://stackoverflow.com/questions/32799614
复制相似问题