AVI AVI - 1 month ago 10
PHP Question

how to send xml to web service with php

I use RESTful webservice and it's show some xml.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <users>
- <user>
<id>1</id>
<name>Mahesh</name>
<profession>Teacher</profession>
</user>
</users>


I want to add new xml, send to the webservice.

$xml =
'<users>
<user>
<id>3</id>
<name>hangga</name>
<profession>IT</profession>
</user>
</users>';


Here my php code :

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://localhost:18080/UserManagement/rest/UserService/users");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/xml')); //setting content type header
curl_setopt($curl, CURLOPT_POST, $xml);//Setting raw post data as xml
$result = curl_exec($curl);
curl_close($curl);
print($result);


But nothing happens, web service not show new XML. My question how to add new xml and send it to web service using php? should I use cUrl? is there another way? I really need help, the answer will very appreciated. Thanks all

Answer
$url = 'http://webservice/example/user';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/xml',                                                                                
    'Content-Length: ' . strlen($data))                                                                       
);   
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$responses = curl_exec($ch);
curl_close($ch);