AVI AVI - 10 months ago 48
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>

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

$xml =

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);

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 Source
$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);