user2822230
user2822230

Reputation: 23

how to post xml with curl with php GET variables?

the script will post xml content to a url, and i need to include the php GET variables (eg. $RegionId), pls advise how to.

 $RegionId = $_GET["RegionId"];

// xml data 


$xml_data ='<AvailabilitySearch>
  <RegionId xmlns="http://www.reservwire.com/namespace/WebServices/Xml">$RegionId</RegionId>
  <HotelId xmlns="http://www.reservwire.com/namespace/WebServices/Xml">0</HotelId>
  <HotelStayDetails xmlns="http://www.reservwire.com/namespace/WebServices/Xml">
</AvailabilitySearch> 
';

// assigning url and posting with curl 

$URL = "http://roomsxmldemo.com/RXLStagingServices/ASMX/XmlService.asmx";

$ch = curl_init($URL);

............
............
............

$RegionId is not posted on the script, how to use the GET or POST variable on that xml content?

Upvotes: 0

Views: 1123

Answers (3)

Mubin
Mubin

Reputation: 4425

Hope this will help You

 $RegionId = $_GET["RegionId"];

    // xml data 


    $xml_data ='<?xml version="1.0" encoding="UTF-8"?><AvailabilitySearch>
      <RegionId xmlns="http://www.reservwire.com/namespace/WebServices/Xml">{$RegionId}</RegionId>
      <HotelId xmlns="http://www.reservwire.com/namespace/WebServices/Xml">0</HotelId>
      <HotelStayDetails xmlns="http://www.reservwire.com/namespace/WebServices/Xml">
    </AvailabilitySearch> 
    ';

    // assigning url and posting with curl 

    $URL = "http://roomsxmldemo.com/RXLStagingServices/ASMX/XmlService.asmx";
    $headers = array(
    "Content-type: text/xml",
    "Content-length: " . strlen($xml_data),
    "Connection: close",
);

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$UR);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$data = curl_exec($ch); 
curl_close($ch);
echo $data;

Upvotes: 1

Andrey Chul
Andrey Chul

Reputation: 146

Well, merely you can wrap your xml content in double quotes and substitute necessary variables.

$id = (isset($_GET['id'])) ? $_GET['id'] : '';

xml_data = "<AvailabilitySearch>
  <RegionId xmlns=\"http://www.reservwire.com/namespace/WebServices/Xml\">$rid</RegionId>
  <HotelId xmlns=\"http://www.reservwire.com/namespace/WebServices/Xml\">0</HotelId>
  <HotelStayDetails xmlns=\"http://www.reservwire.com/namespace/WebServices/Xml\">
</AvailabilitySearch>";

Value of $id variable will be in xml content. And you can post this message. But, don't forget to shield inner double quotes with additional slashes in your xml.

Upvotes: 0

Elon Than
Elon Than

Reputation: 9765

Just add your GET vars to url.

 $URL = "http://roomsxmldemo.com/RXLStagingServices/ASMX/XmlService.asmx?var1=val1&var2=val2";

Upvotes: 0

Related Questions