首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PHP从SOAP响应中获得值?

如何使用PHP从SOAP响应中获得值?
EN

Stack Overflow用户
提问于 2015-09-26 16:54:56
回答 3查看 3K关注 0票数 0

我正在尝试使用PHP从SOAP响应中获得一个值。不管我做了什么,我都无法得到变量中的值。请帮帮忙。

我正在使用WordPress的wp_remote_post()提交表单并得到响应。

代码语言:javascript
复制
$response = wp_remote_post( $url, $args);
$xml = $response['body'];  

下面是SOAP中的响应:

代码语言:javascript
复制
<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>  

以下是我已经尝试过的:

代码语言:javascript
复制
// 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.

代码语言:javascript
复制
 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 } 
EN

回答 3

Stack Overflow用户

发布于 2015-09-26 21:33:23

根据WordPress 员额()函数文档,http响应结果将在数组中。通过对您的var_dump数据,主体键作为有效的XML存在。

您只需从soap:前缀中清除xml即可。

代码语言:javascript
复制
$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

票数 1
EN

Stack Overflow用户

发布于 2015-09-26 17:10:57

有一种方法:

代码语言:javascript
复制
$foo = new SimpleXMLElement($xmlstr);
$bar = json_decode(json_encode($foo));
print_r($bar);

我相信你能搞清楚剩下的。

票数 0
EN

Stack Overflow用户

发布于 2015-09-26 17:17:49

您只需要使用适当的方法从XML字符串中检索有效的对象,如下所示:

代码语言:javascript
复制
$response_body = wp_remote_retrieve_body($response);
$xml  = simplexml_load_string($response_body);
$value = $xml->body->sendtransactionsactionresponse->sendtransactionsactionresult;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32799614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档