2010-12-02 29 views
6

PHP kullanarak bir sunucuya XML verileriyle bir HTTPS POST isteği göndermeye çalışıyorum.PHP https cURL ile XML verilerini gönder

Sunucuya gönderilen her şey kimlik doğrulaması gerektirir, bu nedenle cURL kullanacağım.

Bazı arka plan bilgileri .: XML verileri, sunucunun belirli bir URL'den yerel bir dosyaya dosya yüklemesini istemektir.

Bu API'yi kullanmanın bir kuralı, her istek için içerik türünü application/xml olarak ayarlamanız GEREKLİDİR.

Bu benim yaptığım ama çalışmıyor ne ...

<?php 
$fields = array(
'data'=>'<n1:asset xmlns:n1="http://.....com/"><title>AAAA</title><fileName>http://192.168.11.30:8080/xxx.html</fileName><description>AAAA_desc</description><fileType>HTML</fileType></n1:asset>' 
); 
$fields_string = ""; 
foreach($fields as $key=>$value) { 
$fields_string .= $key.'='.$value.'&'; 
} 
rtrim($fields_string,'&'); 

$url = "https://192.168.11.41:8443/"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_POST, count($fields)); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 

curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERPWD, "admin:12345678"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml', 'Content-length: '. strlen($fields_string))); 

$result = curl_exec($ch); 

curl_close($ch); 

echo $result; 
?> 

Ben XML cevap olsun ya başarılı yüklemek veya başarısız yüklemek için beklenen ediyorum. Ancak bunun yerine bu hata iletisini alıyorum.

HTTP/1.1 415 Desteklenmeyen Medya Türü Sunucusu: Apache-Coyote/1.1 Content-Type: text/xml; charset = UTF-8 İçerik-Uzunluk: 0 Tarih: Per, 2 Aralık 2010 03 : 02: 33 GMT

Dosya türünün doğru olduğundan eminim, XML formatı doğru. Alanları kodlamayı denedim ama işe yaramadı. Başka neler yanlış yapabilirdim?

cevap

0

Belki de application/xml yerine text/xml deneyebilirsiniz?

+0

çalışmak ne – EDDIU

0

Sen orijinal kodda, yapmak zorunda:

curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields['data']); 

veya

curl_setopt($ch, CURLOPT_POSTFIELDS, '<n1:asset xmlns:n1="http://.....com/"><title>AAAA</title><fileName>http://192.168.11.30:8080/xxx.html</fileName><description>AAAA_desc</description><fileType>HTML</fileType></n1:asset>'); 

Ben doğrudan XML göndermek zorunda olduğu anlamına

.

+0

sadece işe yaramadı, o çalıştı vermedi, o çalıştı ile çözmek. Aynı hata mesajıyla cevap verdi. – EDDIU

3

Ben

$xml = $this->getXml(); 
$url = $this->getUrl(); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
             'Content-type: application/xml', 
             'Content-length: ' . strlen($xml) 
            )); 
$output = curl_exec($ch); 
curl_close($ch); 
+0

curl_setopt ($ ch, CURLOPT_POST, 1) arasındaki fark nedir; ve curl_setopt ($ ch, CURLOPT_POST, doğru); Bu CURL'ye tam olarak ne anlatıyor? – TimNguyenBSM