热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将SOAPXML响应转换为PHP对象或数组-convertingSOAPXMLresponsetoaPHPobjectorarray

ImusingcURLtosendanrequesttoaSOAPservice,IsendinPOSTBodytheXMLcontainingparamete

I'm using cURL to send an request to a SOAP service, I send in POST Body the XML containing parameters, in response I receive:

我正在使用cURL向SOAP服务发送请求,我在POST Body中发送包含参数的XML,作为响应,我收到:

Web service: http://lcbtestxmlv2.ivector.co.uk/soap/book.asmx?WSDL

Web服务:http://lcbtestxmlv2.ivector.co.uk/soap/book.asmx?WSDL


    
       
          
             
                
                   true
                   
                
                http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5®iOnid=9
                
                   
                      215
                      1795
                      Hotel Gaddis
                      3.0
                      Egypte
                      Louxor
                      Cet établissement confortable propose un très bon service à un bon rapport qualité-prix. Cet hôtel de 6 étages compte 55 chambres et comprend une terrasse, une réception avec coffre-fort et ascenseur,
                      Cet établissement confortable propose un très bon service à un bon rapport qualité-prix. Cet hôtel de 6 étages compte 55 chambres et comprend une terrasse, une réception avec coffre-fort et ascenseur,...
                      http://lcbtestxml1.ivector.co.uk/content/DataObjects/Property/Image/
                      image_1795_v1.jpg
                      imagethumb_1795_v1.jpg
                      http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5®iOnid=9&propertyid=1795
                      
                         
                            1
                            690039000
                            3
                            Twin/double Room
                            
                            Petit Déjeuner
                            false
                            150.58
                            0
                            150.58
                            2
                            1
                            0
                            
                         
                         
                            1
                            690039001
                            7
                            Twin/double Room
                            
                            Demi-Pension
                            false
                            291.64
                            0
                            291.64
                            2
                            1
                            0
                            
                         
                         
                            1
                            690039002
                            5
                            Double/twin Room
                            
                            Pension Complète
                            false
                            529.22
                            0
                            529.22
                            2
                            1
                            0
                            
                         
                      
                   
                
             
          
       
    

I don't have enough experience with XML data. I spent hours trying to convert the XML response to a PHP object or array, but without any success.

我没有足够的XML数据经验。我花了几个小时尝试将XML响应转换为PHP对象或数组,但没有任何成功。

I need to read all PropertyResults.

我需要阅读所有PropertyResults。

PHP Code:

PHP代码:

$xml = simplexml_load_string($soap_xml_result);

$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->registerXPathNamespace('xsd', 'http://www.w3.org/2001/XMLSchema');

$test = (string) $xml->Body->SearchResponse->SearchResult->SearchURL;
var_export($test);

4 个解决方案

#1


9  

The hint of bksi is not that wrong, however technically as this is XML you only need to access the namespaced elements properly. This works more easy by using an XPath expression and registering the namspace-uri to your own prefix:

bksi的提示并没有错,但从技术上来说,这是XML,你只需要正确访问命名空间元素。通过使用XPath表达式并将namspace-uri注册到您自己的前缀,这可以更轻松地工作:

$soap = simplexml_load_string($soapXMLResult);
$soap->registerXPathNamespace('ns1', 'http://ivectorbookingxml/');
$test = (string) $soap->xpath('//ns1:SearchResponse/ns1:SearchResult/ns1:SearchURL[1]')[0];
var_dump($test);

Output:

输出:

string(100) "http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5®iOnid=9"

If you don't want to use XPath, you need to specify the namespace while you traverse, only the children in the namespace of the element itself are available directly if the element itself is not prefixed. As the root element is prefixed you first need to traverse up to the response:

如果您不想使用XPath,则需要在遍历时指定命名空间,如果元素本身没有前缀,则只有元素本身的命名空间中的子项可以直接使用。由于根元素是前缀的,因此首先需要遍历响应:

$soap     = simplexml_load_string($soapXMLResult);
$respOnse= $soap->children('http://schemas.xmlsoap.org/soap/envelope/')
                     ->Body->children()
                         ->SearchResponse
;

Then you can make use of the $response variable as you know it:

然后你可以使用你知道的$ response变量:

$test = (string) $response->SearchResult->SearchURL;

because that element is not prefixed. As a more complex result is returned this is probably the best because you can easily access all the response values.

因为该元素没有前缀。返回更复杂的结果时,这可能是最好的,因为您可以轻松访问所有响应值。

Your question is similar to:

您的问题类似于:

  • Parse CDATA from a SOAP Response with PHP
  • 使用PHP从SOAP响应中解析CDATA

Maybe the code/descriptions there are helpful, too.

也许代码/描述也有帮助。

#2


9  

You could consider passing the SOAP response through a DOM Document, and then converting it into a simplexml object.

您可以考虑通过DOM Document传递SOAP响应,然后将其转换为simplexml对象。

loadHTML($soap_response);
libxml_clear_errors();
$xml = $doc->saveXML($doc->documentElement);
$xml = simplexml_load_string($xml);
$respOnse= $xml->body->envelope->body->searchresponse;
//print_r($response); exit;
echo $response->searchresult->returnstatus->success;
echo '
'; echo $response->searchresult->searchurl; ?>

However, this may cause problems with special characters in your response, like é and à. Else, it works.

但是,这可能会导致您的回复中出现特殊字符问题,例如é和à。否则,它的工作原理。

#3


2  

Hm. You should use SOAP client to do that, not just send SOAP requests. PHP has integrated SOAP functionalities http://php.net/manual/en/book.soap.php.

嗯。您应该使用SOAP客户端来执行此操作,而不仅仅是发送SOAP请求。 PHP集成了SOAP功能http://php.net/manual/en/book.soap.php。

There are custom soap libraries like NuSOAP http://sourceforge.net/projects/nusoap/.

有像NuSOAP http://sourceforge.net/projects/nusoap/这样的自定义肥皂库。

Most of php frameworks has SOAP libraries too.

大多数php框架也都有SOAP库。

#4


2  

Another solution, the only solution that worked for me :

另一种解决方案,唯一对我有用的解决方案:

$xml = $soap_xml_result;
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", '$1$2$3', $xml);
$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$respOnseArray= json_decode($json, true); // true to have an array, false for an object
print_r($responseArray);

Enjoy :)

请享用 :)


推荐阅读
  • 1.WebServicea.定义:WebService是一种跨编程语言和跨操作系统平台的远程调用技术b.三大技术:XMLXSD,SOAP, ... [详细]
  • Postman 调试 WebService
    Postman调试WebServiceWebServicePostman设置Headers请求头参数Body请求体传参返回结果WebService天气预报Web服务http:ww ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 网络请求模块选择——axios框架的基本使用和封装
    本文介绍了选择网络请求模块axios的原因,以及axios框架的基本使用和封装方法。包括发送并发请求的演示,全局配置的设置,创建axios实例的方法,拦截器的使用,以及如何封装和请求响应劫持等内容。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
  • SOA架构理解理解SOA架构,了解ESB概念,明白SOA与微服务的区别和联系,了解SOA与热门技术的结合与应用。1、面向服务的架构SOASOA(ServiceOrien ... [详细]
  • 在使用豆瓣OAuth登录接口时,我们需要发送这样的HTTPREQUEST请求:GETv2user~meHTTP1.1Host:https:api.douban.com ... [详细]
  • Centos7安装MySql5.6
    如何在CentO ... [详细]
  • .NET最新漏洞CVE20178759 POC已公布,大规模攻击即将来临
    图世界范围内感染FinSpy图(即漏洞利用导致下载的间谍程序)前天微软刚补好的漏洞,昨天fireeye发文,然后各平台开始写 ... [详细]
  • 我正在尝试删除我的文件夹之外的所有电子邮件帐户.收件箱使用python3和exchangelib。在testFolderaccount.rootTopofInformationSt ... [详细]
  • 开发笔记:Java是如何读取和写入浏览器Cookies的
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java是如何读取和写入浏览器Cookies的相关的知识,希望对你有一定的参考价值。首先我 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • SpringMVC接收请求参数的方式总结
    本文总结了在SpringMVC开发中处理控制器参数的各种方式,包括处理使用@RequestParam注解的参数、MultipartFile类型参数和Simple类型参数的RequestParamMethodArgumentResolver,处理@RequestBody注解的参数的RequestResponseBodyMethodProcessor,以及PathVariableMapMethodArgumentResol等子类。 ... [详细]
  • 分布式Dubbo 分布式服务
    分布式,du ... [详细]
author-avatar
mobiledu2502878383
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有