Friday 7 December 2012

Post XML data using CURL - PHP


A couple of methods are available to post an xml data to the server. Here we are showing how to post the xml data in HTTP post method by using CURL and PHP.

Here is the code.

<?PHP
 
// xml data 
 
$xml_data ='<RequestData> 
 
 <userID>'.uniqid().'</userID> 
 
 <lName>'.$lastname.'</lName> 
 
 <fName>'.$firstname.'</fName>
 
 <pass>'.$password.'</pass>
 
 <uGroupID>1</uGroupID>
 
 <rightID>1</rightID>
 
 <userNum>1</userNum>
 
 <emailID>'.$email.'</emailID>
 
 <isConsultant>1</isConsultant>
 
 <passChange>1</passChange>
 
 <passNoExchange>1</passNoExchange>
 
 <disabled>0</disabled>
 
 <saFailAttempt>1</saFailAttempt>
 
</RequestData>
 
';
 
// assigning url and posting with curl 
 
$URL = "http://172.16.0.44/RespackService/RespackService.svc/AddUser";
 
$ch = curl_init($URL);
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 
curl_setopt($ch, CURLOPT_POST, 1);
 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
 
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$output = curl_exec($ch);
 
curl_close($ch);
 
// prints the output 
 
print_r($output);exit;
 

?>

No comments:

Post a Comment