php将SOAP的响应数据转换为array

947 阅读1分钟

#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"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}