Home > Net >  Implement XML post request wih PHP
Implement XML post request wih PHP

Time:08-12

I want to call a API call using PHP. It works well when I call the POST request through Postman client. But how can I get same response with PHP?

Postman POST request url:

https://apps8.tflite.com/PublicServiceU/Public.svc/asmx

Postman headers:

Content-Type: text/xml;charset=UTF-8
Accept-Encoding: gzip,deflate
SOAPAction: https://apps.contact-technology.co.nz/TakeFlitePublicService/PerformSearch

Postman Body:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Header/>  
      <Body>  
        <PerformSearch xmlns="https://apps.contact-technology.co.nz/TakeFlitePublicService/">   
            <SearchClassInstance >  
               <Adults>1</Adults>                          
               <Database>devhinter</Database>  
               <From>Cairns</From>  
               <To>Coen</To>  
               <Date>2022-11-22</Date>
            </SearchClassInstance>  
                <AgentLogin>MyUserName</AgentLogin>
                <AgentPassword>MyPassword</AgentPassword>  
         </PerformSearch>  
      </Body>  
   </Envelope>

The response look like this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <PerformSearchResponse xmlns="https://apps.contact-technology.co.nz/TakeFlitePublicService/">
            <PerformSearchResult>
                <HasFlights>true</HasFlights>
                <OutgoingFlights>
                    <FlightInfo>
                        <ExtensionData/>
                        <Adults>1</Adults>
                        <ArrivalTime>2022-11-22T09:15:00</ArrivalTime>
                        <ArriveTimeInlcZoneOffset>2022-11-22T09:15:00</ArriveTimeInlcZoneOffset>
                        <ChargeDoubleFirstPax>false</ChargeDoubleFirstPax>
                        <CheckInTime>2022-11-22T06:45:00</CheckInTime>
                        <CheckInTimeInlcZoneOffset>2022-11-22T06:45:00</CheckInTimeInlcZoneOffset>
                        <Children>0</Children>
                        <CustomPax>0</CustomPax>
                        <DepartTime>2022-11-22T07:30:00</DepartTime>
                        <DepartTimeInlcZoneOffset>2022-11-22T07:30:00</DepartTimeInlcZoneOffset>
                        <EarlyBird1>
                            <ExtensionData/>
                            <Adult>0</Adult>
                            <Allowed>false</Allowed>
                            <Child>0</Child>
                            <Custom>0</Custom>
                            <Explanation/>
                            <FareClass>1</FareClass>
                            <ID>0</ID>
                        </EarlyBird1>
                        <EarlyBird2>
                            <ExtensionData/>
                            <Adult>0</Adult>
                            <Allowed>false</Allowed>
                            <Child>0</Child>
                            <Custom>0</Custom>
                            <Explanation/>
                            <FareClass>2</FareClass>
                            <ID>0</ID>
                        </EarlyBird2>
                        <FlightNumberString>171</FlightNumberString>
                        <FloatTypeAny>false</FloatTypeAny>
                        <From>Cairns</From>
                        <HasEmptyFlights>true</HasEmptyFlights>
                        <InactiveOnly>false</InactiveOnly>
                        <InactiveOnlyReason/>
                        <Leg>1</Leg>
                        <MultiSector>false</MultiSector>
                        <NoDiscount>false</NoDiscount>
                        <NoDropOffs>true</NoDropOffs>
                        <NoPickups>true</NoPickups>
                        <PackageFlightNoAdultFare>0</PackageFlightNoAdultFare>
                        <PackageFlightNoChildFare>0</PackageFlightNoChildFare>
                        <PackageFlightNoFaresUsed>false</PackageFlightNoFaresUsed>
                        <PassengerList/>
                        <Passengers>1</Passengers>
                        <Route>
                            <ExtensionData/>
                            <ArrivalTime>2022-11-22T09:15:00</ArrivalTime>
                            <BlockedString>Cairns &gt; Coen</BlockedString>
                            <DepartTime>2022-11-22T07:30:00</DepartTime>
                            <FlightDuration>105</FlightDuration>
                            <SectorSummary>Cairns &gt; Coen (171)</SectorSummary>
                            <Sectors>
                                <RouteEngineSector>
                                    <ExtensionData/>
                                    <ArrivalTime>2022-11-22T09:15:00</ArrivalTime>
                                    <DepartTime>2022-11-22T07:30:00</DepartTime>
                                    <FlightNumber>171</FlightNumber>
                                    <From>Cairns</From>
                                    <InactiveOnlyReason/>
                                    <Info>
                                        <ExtensionData/>
                                        <Adult>599.00</Adult>
                                        <AdultFare>609.00</AdultFare>
                                        <AdultWebFare>599.00</AdultWebFare>
                                        <AircraftIDs>
                                            <int>4</int>
                                        </AircraftIDs>
                                        <Arrive>2022-11-22T09:15:00</Arrive>
                                        <ArriveLocal>0001-01-01T00:00:00</ArriveLocal>
                                        <BackpackerFare>0</BackpackerFare>
                                        <BackpackerSeatsTaken xsi:nil="true"/>
                                        <ChargeDoubleFirstPax>false</ChargeDoubleFirstPax>
                                        <CheckInOffset>0</CheckInOffset>
                                        <Child>469.00</Child>
                                        <ChildFare>479.00</ChildFare>
                                        <ChildWebFare>469.00</ChildWebFare>
                                        <CustomFare>0</CustomFare>
                                        <DefaultAirline>Hinterland Aviation </DefaultAirline>
                                        <DefaultAirlineTimezone>E. Australia Standard Time</DefaultAirlineTimezone>
                                        <DefaultBagWeight>0</DefaultBagWeight>
                                        <Depart>2022-11-22T07:30:00</Depart>
                                        <DepartLocal>0001-01-01T00:00:00</DepartLocal>
                                        <ExtrasAllowed>true</ExtrasAllowed>
                                        <FlightNumber>171</FlightNumber>
                                        <Float>false</Float>
                                        <From>Cairns</From>
                                        <FuelSurcharge>0</FuelSurcharge>
                                        <GateControlUser/>
                                        <International>false</International>
                                        <MaxBackpackers>0</MaxBackpackers>
                                        <MaxBagWeight>20</MaxBagWeight>
                                        <MaxInfantsAllowed>2</MaxInfantsAllowed>
                                        <MinimumPax>0</MinimumPax>
                                        <NoDirectFlights>false</NoDirectFlights>
                                        <NoDiscounts>false</NoDiscounts>
                                        <NoDropOffs>true</NoDropOffs>
                                        <NoEarlybirds>false</NoEarlybirds>
                                        <NoFreight>false</NoFreight>
                                        <NoPassengers>false</NoPassengers>
                                        <NoPassengersPublic>false</NoPassengersPublic>
                                        <NoPassengersPublicExcludingAgents>false</NoPassengersPublicExcludingAgents>
                                        <NoPickups>true</NoPickups>
                                        <NoSpecials>false</NoSpecials>
                                        <Office>Coen</Office>
                                        <Seats>11</Seats>
                                        <SeatsAvailable>11</SeatsAvailable>
                                        <SeatsTaken>0</SeatsTaken>
                                        <SeatsUsedByAgent>0</SeatsUsedByAgent>
                                        <SignedOff>false</SignedOff>
                                        <StatedWeightIncrement>0</StatedWeightIncrement>
                                        <TSANoFly>false</TSANoFly>
                                        <TermsID>2221961</TermsID>
                                        <To>Coen</To>
                                        <TotalEarlybirds>0</TotalEarlybirds>
                                        <TotalInfants>0</TotalInfants>
                                        <TotalSpecials>0</TotalSpecials>
                                        <TravelID>2945272</TravelID>
                                        <WeightAvailable>1662</WeightAvailable>
                                        <WeightMax>1662</WeightMax>
                                        <WeightTaken>0</WeightTaken>
                                        <ZonedFlight>false</ZonedFlight>
                                    </Info>
                                    <RouteName>OI 171/172</RouteName>
                                    <SectorNumber>1</SectorNumber>
                                    <SectorString>sector 1 (Cairns to Coen - 171 (2945272))</SectorString>
                                    <To>Coen</To>
                                    <TravelID>2945272</TravelID>
                                </RouteEngineSector>
                            </Sectors>
                            <StandardPricePerAdult>599.00</StandardPricePerAdult>
                            <StandardPricePerChild>469.00</StandardPricePerChild>
                            <StandardPricePerCustom>0</StandardPricePerCustom>
                            <TravelIDs>2945272</TravelIDs>
                        </Route>
                        <SpecialFare>
                            <ExtensionData/>
                            <Adult>0</Adult>
                            <Allowed>false</Allowed>
                            <Child>0</Child>
                            <Custom>0</Custom>
                            <Explanation/>
                            <FareClass>4</FareClass>
                            <ID>0</ID>
                            <TotalPrice>0</TotalPrice>
                        </SpecialFare>
                        <StandardAdultFare>549.00</StandardAdultFare>
                        <StandardChildFare>549.00</StandardChildFare>
                        <StandardCustomFare>0</StandardCustomFare>
                        <ThroughFares>
                            <ExtensionData/>
                            <Adult>549.00</Adult>
                            <Allowed>true</Allowed>
                            <Backpacker>549.00</Backpacker>
                            <BackpackerChild>549.00</BackpackerChild>
                            <Child>549.00</Child>
                            <Custom>0</Custom>
                            <Explanation/>
                            <ID>65</ID>
                            <InfantFare>0</InfantFare>
                            <TotalPrice>549.00</TotalPrice>
                        </ThroughFares>
                        <To>Coen</To>
                        <TotalSurcharges_Earlybird1>0</TotalSurcharges_Earlybird1>
                        <TotalSurcharges_Earlybird2>0</TotalSurcharges_Earlybird2>
                        <TotalSurcharges_Special>0</TotalSurcharges_Special>
                        <TotalSurcharges_Standard>0</TotalSurcharges_Standard>
                        <TotalTaxes_Earlybird1>0</TotalTaxes_Earlybird1>
                        <TotalTaxes_Earlybird2>0</TotalTaxes_Earlybird2>
                        <TotalTaxes_Special>0</TotalTaxes_Special>
                        <TotalTaxes_Standard>0</TotalTaxes_Standard>
                        <TravelIDs>2945272</TravelIDs>
                        <WeightAvailable>1662</WeightAvailable>
                        <WeightMaxAllowed>1662</WeightMaxAllowed>
                        <ZoneOffsetArrive>0</ZoneOffsetArrive>
                        <ZoneOffsetDepart>0</ZoneOffsetDepart>
                        <ZonedFlightAll>false</ZonedFlightAll>
                        <ZonedFlightAny>false</ZonedFlightAny>
                    </FlightInfo>
                </OutgoingFlights>
                <PackageID>0</PackageID>
                <PackageMode>false</PackageMode>
                <ReturnFlights/>
                <CreditCardSurcharge>2.00000</CreditCardSurcharge>
            </PerformSearchResult>
        </PerformSearchResponse>
    </s:Body>
</s:Envelope>

This is my PHP code:

<?php

$url = "https://apps8.tflite.com/PublicServiceU/Public.svc/asmx";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, "Corp2:cairns2");
$headers = array(
    "Content-type: text/xml;charset=utf-8",
    "Accept-Encoding: gzip,deflate",
    "SOAPAction: https://apps.contact-technology.co.nz/TakeFlitePublicService/PerformSearch"
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = '
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
<Header/>  
      <Body>  
        <PerformSearch xmlns="https://apps.contact-technology.co.nz/TakeFlitePublicService/">   
            <SearchClassInstance >  
               <Adults>1</Adults>                          
               <Database>devhinter</Database>  
               <From>MyUserName</From>  
               <To>MyPassword</To>  
               <Date>2022-11-22</Date>
            </SearchClassInstance>  
                <AgentLogin>Corp2</AgentLogin>
                <AgentPassword>cairns2</AgentPassword>  
         </PerformSearch>  
      </Body>  
   </Envelope>';

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
die();
?>

the result for var_dump look like this: enter image description here

How can I correctly implement this? I want to get a readable response same as getting from Postman client.

CodePudding user response:

It looks like you're asking for the response to be compressed ("Accept-Encoding: gzip,deflate") and the other side is friendly enough to do that for you. You can either:

  • Remove that request header. The response will now not be compressed.
  • Use curl_setopt to set the CURLOPT_ENCODING setting instead of adding an Accept-Encoding header manually; curl will now take care of uncompressing the response if needed.
  • Call gzinflate or gzuncompress on the response if you want to do this yourself for whatever reason. If you do this, make sure to check the response headers to confirm that the response is indeed compressed the way you expect; the Accept-Encoding header is a request which the server is free to ignore.
  • Related