2010-12-02 8 views
6

PHP를 사용하여 XML 데이터와 함께 HTTPS POST 요청을 서버에 보내려고합니다.PHP https cURL을 사용하여 XML 데이터 게시

서버에 보내는 모든 것은 인증이 필요하므로 cURL을 사용합니다.

일부 배경 정보 : XML 데이터는 특정 URL에서 로컬 저장소로 파일을 업로드하도록 서버에 요청하는 것입니다.

이 API를 사용하는 규칙 중 하나는 각 요청의 콘텐츠 유형을 application/xml으로 설정해야한다는 것입니다.

이 내가 해봤지만 작동하지 않는 것입니다 ...

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

은 내가의 XML 응답을 얻을 중 하나를 성공적으로 업로드하거나 업로드 실패 할 것으로 예상하고있다. 대신이 오류 메시지가 나타납니다.

HTTP/1.1 415 지원되지 않는 미디어 유형 서버 : 아파치 - 코요테/1.1 콘텐츠 유형 : 텍스트/XML을; 문자셋 = UTF-8 콘텐츠 길이 : 0 날짜 : 12월 2일 (목) 2010 03 : 02 : 33 GMT

파일 형식이 올바른지, XML 형식이 올바른지 확인하십시오. 필드를 urlencode 시도했지만 작동하지 않습니다. 그 밖의 내가 잘못했을 수도있는 것은 무엇입니까?

답변

0

아마도 application/xml 대신 text/xml을 사용할 수 있습니까?

+0

가 작동하지 둘 – EDDIU

0

당신은 당신이 원래 코드에서해야 할 :

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

또는

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

내가 직접 XML을 보낼 필요가 있다는 것을 의미

.

+0

그냥 작동하지 않았다 것을 시도하지 않은 것을 시도로 해결한다. 동일한 오류 메시지로 응답했습니다. – EDDIU

3

나는

$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);의 차이점은 무엇입니까? 및 curl_setopt ($ ch, CURLOPT_POST, true); 그게 CURL에 정확히 무엇을 말합니까? – TimNguyenBSM

관련 문제