AVI AVI - 5 months ago 59
PHP Question

Error HTTP Status 415 - Unsupported Media Type when send XML from php to RESTful

I have RESTful web service deploy on jboss EAP. In other, I create php file for make a XML and I want send(POST) them to RESTful.

The RESTful webservice show like this :

<name>Moyes Chuck</name>
<name>Van Gaal</name>

And create postxml.php :

$xml = new SimpleXMLElement('<sample/>');
$track = $xml->addChild('user');
$track->addChild('id', "3");
$track->addChild('name', "Brody Ben");
$track->addChild('profession', "Manager");

in same file(postxml.php), this code I wrote to post to RESTful:

$service_url1 = '';
$curl1 = curl_init($service_url1);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_POST, 1);
curl_setopt($curl1, CURLOPT_POSTFIELDS,$arr);
echo $curl1_response = curl_exec($curl1);

But when I run postxml.php on browser http://localhost:82/test3/postxml.php, it show an error :

HTTP Status 415 - Unsupported Media Type.

JBWEB000069: description JBWEB000135: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.


remove $arr=array("key"=>$xml); and just add this code and will be work fine

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/xml',                                                                                
    'Content-Length: ' . strlen($xml))