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

发布于 2024-12-28 01:22:49 字数 277 浏览 1 评论 0原文

我目前正在处理一个过时的支付处理器,它使得连接到他们的服务变得尽可能困难(包括自定义客户端 SSL 证书,带有密码,再加上基本的 HTTP 身份验证)。长话短说,我无法使用 SoapClient 发出请求,但我可以使用 cURL 来做到这一点。

我现在有字符串形式的响应,我可以使用 SoapClient 来解析它吗?我不想将其作为常规 XML 手动解析,因为我必须重复很多功能,例如在查找 时抛出合理的异常,例如例子。

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). Long story short, I can't use SoapClient to make the request, but I have been able to do it with cURL.

I now have the response in a string, can I use SoapClient to parse it? 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼中杀气 2025-01-04 01:22:49

不,你不能。

(只是为了后代回答这个问题。由于缺乏相反的证据,您显然不能使用 SoapClient 来解析您已有的 SOAP 响应)

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)

挽清梦 2025-01-04 01:22:49

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

You can define context using context option of SoapClient to tell SoapClient to use SSL certificates etc. Context may be created using stream_context_create with lots of options

不及他 2025-01-04 01:22:49

让我们再想象一下您调用了 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...

?>

这只是一些示例/伪代码(未经测试并且无法按原样工作)。我的观点是,您手动获取了 SOAP 响应,因此现在您所要做的就是解析它。 SimpleXML 是您可以使用的一个解决方案这样做。

Let's for a second imagine you had called SoapClient::__doRequest() and it returned your XML SOAP response into a variable called $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. SimpleXML is one solution you could use to do that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文