<soap:Envelope xmlns:soap="http://...">
最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • php将SOAP的响应数据转换为array

    正文概述 转载于:掘金(西门族长)   2021-03-10   827

    #1、SOAP返回的数据格式

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <soap:Header>
            <OGHeader transactionID="000032" timeStamp="2021-03-09T00:00:00" xmlns="http://webservices.micros.com/og/4.3/Core/">
                <Origin entityID="ORS" systemType="ORS" />
                <Destination entityID="PETRAVEL" systemType="WEB" />
            </OGHeader>
        </soap:Header>
        <soap:Body>
            <CreateBookingResponse xmlns:hc="http://webservices.micros.com/og/4.3/HotelCommon/" xmlns:c="http://webservices.micros.com/og/4.3/Common/" xmlns:r="http://webservices.micros.com/og/4.3/Reservation/" xmlns="http://webservices.micros.com/ows/5.1/Reservation.wsdl">
                <Result resultStatusFlag="SUCCESS" />
                <HotelReservation SessionAction="BOOKING">
                    <r:UniqueIDList>
                        <c:UniqueID type="INTERNAL">M_20210309234113623663PET</c:UniqueID>
                    </r:UniqueIDList>
                    <r:RoomStays>
                        <hc:RoomStay>
                            <hc:RatePlans>
                                <hc:RatePlan ratePlanCode="9VWBAR" qualifyingIdType="TRAVEL_AGENT" qualifyingIdValue="PETRAVEL" childQualifyingIdValue="PETRAVEL" />
                            </hc:RatePlans>
                            <hc:RoomTypes>
                                <hc:RoomType roomTypeCode="KC" />
                            </hc:RoomTypes>
                            <hc:RoomRates>
                                <hc:RoomRate roomTypeCode="KC" ratePlanCode="9VWBAR">
                                    <hc:Rates>
                                        <hc:Rate effectiveDate="2021-03-09">
                                            <hc:Base>1152.9</hc:Base>
                                        </hc:Rate>
                                    </hc:Rates>
                                </hc:RoomRate>
                            </hc:RoomRates>
                            <hc:GuestCounts>
                                <hc:GuestCount ageQualifyingCode="ADULT" />
                                <hc:GuestCount ageQualifyingCode="CHILD" />
                            </hc:GuestCounts>
                            <hc:TimeSpan>
                                <hc:StartDate>2021-03-09T00:00:00</hc:StartDate>
                                <hc:EndDate>2021-03-10T00:00:00</hc:EndDate>
                            </hc:TimeSpan>
                            <hc:Guarantee guaranteeType="TAGTD" />
                            <hc:HotelReference chainCode="CCM" hotelCode="VMRMO" />
                            <hc:Total>1152.9</hc:Total>
                            <hc:Comments>
                                <hc:Comment guestViewable="true">
                                    <hc:Text>成人2名;</hc:Text>
                                </hc:Comment>
                            </hc:Comments>
                            <hc:ExpectedCharges>
                                <hc:ChargesForPostingDate>
                                    <hc:RoomRateAndPackages TotalCharges="0" />
                                    <hc:TaxesAndFees TotalCharges="0" />
                                </hc:ChargesForPostingDate>
                            </hc:ExpectedCharges>
                        </hc:RoomStay>
                    </r:RoomStays>
                    <r:ResGuests>
                        <r:ResGuest resGuestRPH="0">
                            <r:Profiles>
                                <Profile xmlns="http://webservices.micros.com/og/4.3/Name/">
                                    <Customer>
                                        <PersonName>
                                            <c:nameTitle>Mr.</c:nameTitle>
                                            <c:firstName>SHUHAN</c:firstName>
                                            <c:lastName>ZHANG</c:lastName>
                                        </PersonName>
                                        <NativeName languageCode="ZH-S">
                                            <c:lastName>ZHANG SHUHAN</c:lastName>
                                        </NativeName>
                                    </Customer>
                                </Profile>
                            </r:Profiles>
                        </r:ResGuest>
                    </r:ResGuests>
                </HotelReservation>
            </CreateBookingResponse>
        </soap:Body>
    </soap:Envelope>
    

    #2、编写转换函数

    	/**
         * @desc 将SOAP响应消息转为数组
         * @param $xmlResp
         * @return mixed
         */
        private function soapXmlParser($xmlResp)
        {
            $xml_parser = xml_parser_create();
            if (!xml_parse($xml_parser, $xmlResp, true)) {
                \Log::error($xmlResp);
                xml_parser_free($xml_parser);//释放 XML 解析器
                return false;
            }
            // SimpleXML seems to have problems with the colon ":" in the <xxx:yyy> response tags, so take them out
            $xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xmlResp);
            $xml = simplexml_load_string($xml);
            return json_decode(json_encode($xml), true);
        }
    
        /**
         * 项目中使用的版本
         * @desc 将SOAP响应消息转为数组
         * @param $xmlResp
         * @return bool|mixed
         */
        private function soapXmlParser2($xmlResp)
        {
            $xml_parser = xml_parser_create();
            if (!xml_parse($xml_parser, $xmlResp, true)) {
                \Log::error($xmlResp);
                xml_parser_free($xml_parser);//释放 XML 解析器
                return false;
            }
            $xml = preg_replace('/\sxmlns="(.*?)"/', ' _xmlns="${1}"', $xmlResp);
            $xml = preg_replace('/<(\/)?(\w+):(\w+)/', '<${1}${2}_${3}', $xml);
            $xml = preg_replace('/(\w+):(\w+)="(.*?)"/', '${1}_${2}="${3}"', $xml);
            return (json_decode(json_encode(simplexml_load_string($xml)), true));
        }
        
    

    #3、解析结果

    {
        "soapHeader": {
            "OGHeader": {
                "@attributes": {
                    "transactionID": "000032",
                    "timeStamp": "2021-03-09T00:00:00"
                },
                "Origin": {
                    "@attributes": {
                        "entityID": "ORS",
                        "systemType": "ORS"
                    }
                },
                "Destination": {
                    "@attributes": {
                        "entityID": "PETRAVEL",
                        "systemType": "WEB"
                    }
                }
            }
        },
        "soapBody": {
            "CreateBookingResponse": {
                "Result": {
                    "@attributes": {
                        "resultStatusFlag": "SUCCESS"
                    }
                },
                "HotelReservation": {
                    "@attributes": {
                        "SessionAction": "BOOKING"
                    },
                    "rUniqueIDList": {
                        "cUniqueID": "M_20210309234113623663PET"
                    },
                    "rRoomStays": {
                        "hcRoomStay": {
                            "hcRatePlans": {
                                "hcRatePlan": {
                                    "@attributes": {
                                        "ratePlanCode": "9VWBAR",
                                        "qualifyingIdType": "TRAVEL_AGENT",
                                        "qualifyingIdValue": "PETRAVEL",
                                        "childQualifyingIdValue": "PETRAVEL"
                                    }
                                }
                            },
                            "hcRoomTypes": {
                                "hcRoomType": {
                                    "@attributes": {
                                        "roomTypeCode": "KC"
                                    }
                                }
                            },
                            "hcRoomRates": {
                                "hcRoomRate": {
                                    "@attributes": {
                                        "roomTypeCode": "KC",
                                        "ratePlanCode": "9VWBAR"
                                    },
                                    "hcRates": {
                                        "hcRate": {
                                            "@attributes": {
                                                "effectiveDate": "2021-03-09"
                                            },
                                            "hcBase": "1152.9"
                                        }
                                    }
                                }
                            },
                            "hcGuestCounts": {
                                "hcGuestCount": [
                                    {
                                        "@attributes": {
                                            "ageQualifyingCode": "ADULT"
                                        }
                                    },
                                    {
                                        "@attributes": {
                                            "ageQualifyingCode": "CHILD"
                                        }
                                    }
                                ]
                            },
                            "hcTimeSpan": {
                                "hcStartDate": "2021-03-09T00:00:00",
                                "hcEndDate": "2021-03-10T00:00:00"
                            },
                            "hcGuarantee": {
                                "@attributes": {
                                    "guaranteeType": "TAGTD"
                                }
                            },
                            "hcHotelReference": {
                                "@attributes": {
                                    "chainCode": "CCM",
                                    "hotelCode": "VMRMO"
                                }
                            },
                            "hcTotal": "1152.9",
                            "hcComments": {
                                "hcComment": {
                                    "@attributes": {
                                        "guestViewable": "true"
                                    },
                                    "hcText": "成人2名;"
                                }
                            },
                            "hcExpectedCharges": {
                                "hcChargesForPostingDate": {
                                    "hcRoomRateAndPackages": {
                                        "@attributes": {
                                            "TotalCharges": "0"
                                        }
                                    },
                                    "hcTaxesAndFees": {
                                        "@attributes": {
                                            "TotalCharges": "0"
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "rResGuests": {
                        "rResGuest": {
                            "@attributes": {
                                "resGuestRPH": "0"
                            },
                            "rProfiles": {
                                "Profile": {
                                    "Customer": {
                                        "PersonName": {
                                            "cnameTitle": "Mr.",
                                            "cfirstName": "SHUHAN",
                                            "clastName": "ZHANG"
                                        },
                                        "NativeName": {
                                            "@attributes": {
                                                "languageCode": "ZH-S"
                                            },
                                            "clastName": "ZHANG SHUHAN"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    起源地下载网 » php将SOAP的响应数据转换为array

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元