Hi,

Can anyone please tell me how to do a REST request to a URL using method POST and NOT GET.

I know I have to pass XML to the URL but I really don't know where to start actually sending the request.

I have tried looking at loads of sites through Google searches but can't get it. Lots of GET tutorials but none on POST.

Many thanks.

Hi Shanti Chepuru,

Thanks for the link. I hadn't come across that specific page yet. Unfortunately the code is not working. When I echo the variable that should hold the response XML all I get is the exact same XML as I input.

Maybe you can see something wrong with my code.

//set POST variables
$url = 'http://xmltravel.com/FindAndBook';

$xml = '
<?xml version="1.0" encoding="utf-8"?>
<FAB_PkgAvailRQ Target="test" Version="2002A" xmlns="http://www.xmltravel.com/fab/2002/09">
  <SyndicatorInfo SyndicatorId="secret" SyndicatorPassword="secret" />
  <SessionInfo CreateNewSession="true" />
  <HolidaySearchRequest ResponseTimeoutSecs="120" ExcludeNonBookableSuppliers="true">
    <ResultSetPreferences SortCode="cost" MaxItems="20" />
    <InitialViewInfo Offset="0" Length="50" />
    <SearchCriteria FlightOnly="true" OneWayOnly="false" NumberOfAdults="2" NumberOfChildren="0">
      <DepartureDateRange StartDate="20081014" EndDate="20081024" />
      <DepartureAirports>
        <Airport>LGW</Airport>
      </DepartureAirports>
      <DestinationChoice>
        <DestinationAirports>
          <Airport>MAD</Airport>
        </DestinationAirports>
      </DestinationChoice>
      <HolidayDuration MinNumberOfNights="7" MaxNumberOfNights="7" />
    </SearchCriteria>
  </HolidaySearchRequest>
</FAB_PkgAvailRQ>
';

// Get the curl session object
$session = curl_init($url);

// Tell curl to use HTTP POST
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $xml);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($session);
curl_close($session); 

echo $response;

The output of variable response is:

<?xml version="1.0" encoding="utf-8"?> <FAB_PkgAvailRQ Target="test" Version="2002A" xmlns="http://www.xmltravel.com/fab/2002/09">  <SyndicatorInfo SyndicatorId="secret" SyndicatorPassword="secret" />  <SessionInfo CreateNewSession="true" />  <HolidaySearchRequest ResponseTimeoutSecs="120" ExcludeNonBookableSuppliers="true">  <ResultSetPreferences SortCode="cost" MaxItems="20" />  <InitialViewInfo Offset="0" Length="50" />  <SearchCriteria FlightOnly="true" OneWayOnly="false" NumberOfAdults="2" NumberOfChildren="0">  <DepartureDateRange StartDate="20081014" EndDate="20081024" />  <DepartureAirports>  <Airport>LGW</Airport>  </DepartureAirports>  <DestinationChoice>  <DestinationAirports>  <Airport>MAD</Airport>  </DestinationAirports>  </DestinationChoice>  <HolidayDuration MinNumberOfNights="7" MaxNumberOfNights="7" />  </SearchCriteria>  </HolidaySearchRequest> </FAB_PkgAvailRQ>

I have been reading so many websites and Have tried many variations on the code above but still it is not working.

Any tips on this would be great as I'm stumped.


Many thanks

Hi, in case anyone comes across this in the future I have included the working version of my code below. Just as a summary; I am POSTing an xml string to a URL and storing the response, also an xml string in a variable.

//set URL
$url = 'http://xmltravel.com/FindAndBook';

//set xml request
$xml = '
<FAB_PkgAvailRQ Target="test" Version="2002A" xmlns="http://www.xmltravel.com/fab/2002/09"><SyndicatorInfo SyndicatorId="secret" SyndicatorPassword="secret"/><SessionInfo CreateNewSession="true"/><HolidaySearchRequest ResponseTimeoutSecs="120" ExcludeNonBookableSuppliers="true"><ResultSetPreferences SortCode="cost" MaxItems="5"/><InitialViewInfo Offset="0" Length="50"/><SearchCriteria FlightOnly="true" OneWayOnly="true" NumberOfAdults="1"><DepartureDateRange StartDate="20081014" EndDate="20081018"/><DepartureAirports><Airport>ALC</Airport></DepartureAirports><DestinationChoice><DestinationAirports><Airport>BRS</Airport></DestinationAirports></DestinationChoice>      <Suppliers><Supplier>EZJ</Supplier></Suppliers></SearchCriteria></HolidaySearchRequest></FAB_PkgAvailRQ>
';

//$xml = urlencode($xml);

// Get the curl session object
$session = curl_init($url);

// set url to post to 
//curl_setopt($session, CURLOPT_URL,$url);
// Tell curl to use HTTP POST;
curl_setopt ($session, CURLOPT_POST, true);
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $xml);
// Tell curl not to return headers, but do return the response
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// allow redirects 
//curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);

$response = curl_exec($session);
//print_r ($response);
curl_close($session);
This question has already been answered. Start a new discussion instead.