簡體   English   中英

錯誤HTTP狀態415-將XML從php發送到RESTful時,媒體類型不受支持

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

我在jboss EAP上部署了RESTful Web服務。 在其他情況下,我創建用於生成XML的php文件,並希望將它們發送(發布)到RESTful。

RESTful http://192.168.0.191:8080/UserManagement/rest/UserService/users/網絡服務顯示如下:

<sample>
  <user>
    <id>1</id>
    <name>Moyes Chuck</name>
    <profession>Teacher</profession>
  </user>
  <user>
    <id>2</id>
    <name>Van Gaal</name>
    <profession>Driver</profession>
  </user>
</sample>

並創建postxml.php:

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

在同一文件(postxml.php)中,我編寫了以下代碼以發布到RESTful:

$service_url1 = 'http://192.168.0.191:8080/UserManagement/rest/UserService/users/';
$curl1 = curl_init($service_url1);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
$arr=array("key"=>$xml);
curl_setopt($curl1, CURLOPT_POST, 1);
curl_setopt($curl1, CURLOPT_POSTFIELDS,$arr);
echo $curl1_response = curl_exec($curl1);
curl_close($curl1);

但是當我在瀏覽器http:// localhost:82 / test3 / postxml.php上運行postxml.php時,顯示錯誤:

HTTP狀態415-不支持的媒體類型。
JBWEB000069:說明JBWEB000135:服務器拒絕了此請求,因為請求實體的格式不受請求的資源所請求的方法的支持。

刪除$arr=array("key"=>$xml); 只需添加此代碼,就可以正常工作

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

您是POST荷蘭國際集團類型的對象SimpleXMLElement到Web服務。

改為發送xml字符串

$arr=array("key"=>$xml->asXML());

如果嘗試

print $xml->asXML();

您將看到實際的XML字符串,這是您需要發送到Web服務的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM