简体   繁体   English

我可以使用PHP的SoapClient来解析SOAP响应,而无需发出HTTP请求吗?

[英]Can I use PHP's SoapClient to parse a SOAP response, without making the HTTP request?

I'm currently dealing with an archaic payment processor that makes connecting to their service as hard as possible (including a custom client SSL cert, with a password, plus basic HTTP Auth after that). 我目前正在与一个古老的支付处理器打交道,该处理器使与他们的服务的连接尽可能地困难(包括自定义客户端SSL证书,密码和此后的基本HTTP Auth)。 Long story short, I can't use SoapClient to make the request, but I have been able to do it with cURL. 长话短说,我不能使用SoapClient发出请求,但是我已经可以使用cURL做到这一点。

I now have the response in a string, can I use SoapClient to parse it? 我现在有一个字符串形式的响应,可以使用SoapClient进行解析吗? I'd rather not have to parse it manually as a regular XML, since I'd have to duplicate a lot of functionality, like throwing a sensible exception when finding a <SOAP:Fault> , for example. 我宁愿不必手动将其解析为常规XML,因为我不得不复制很多功能,例如,在找到<SOAP:Fault>时抛出明智的异常。

No, you can't. 不,你不能。

(just answering this for posterity. Based on the lack of evidence to the contrary, you apparently can't use SoapClient to parse a SOAP response you already have) (只是为了后代而回答。基于相反的证据,显然您不能使用SoapClient来解析已经拥有的SOAP响应)

您可以使用SoapClient context选项定义上下文,以告诉SoapClient使用SSL证书等。可以使用带有很多选项的 stream_context_create创建上下文

Let's for a second imagine you had called SoapClient::__doRequest() and it returned your XML SOAP response into a variable called $response . 让我们再想象一下,您曾经调用了SoapClient :: __ doRequest(),它将XML SOAP响应返回到名为$ response的变量中。

<?php

   //LOAD RESPONSE INTO SIMPLEXML
   $xml = simplexml_load_string($response);

   //REGISTER NAMESPACES
   $xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
   $xml->registerXPathNamespace('somenamespace', 'http://www.somenamespace/schema/');
   //...REGISTER OTHER NAMESPACES HERE...

   //LOOP THROUGH AND GRAB DATA FROM A NAMESPACE
   foreach($xml->xpath('//somenamespace:MessageHeader') as $header)
   {
      echo($header->xpath('//somenamespace:MyData')); 
   }

   //...ETC...

?>

That is just some example/pseudo code (not tested and won't work as-is). 那只是一些示例/伪代码(未经测试,无法按原样工作)。 My point is that you manually acquired the SOAP response so now all you have to do is parse it. 我的观点是,您手动获取了SOAP响应,因此现在您所要做的就是解析它。 SimpleXML is one solution you could use to do that. SimpleXML是您可以用来实现此目的的一种解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM