2012-02-29 2 views
0

XML과 이미지를 함께 사용하여 편안한 웹 서비스를 제출해야합니다. 이것이 말풍선을 사용하여 가능합니까?웹 서비스에 멀티 파트와 XML을 함께 제출하는 PHP

웹에서 사용할 수있는 다양한 예제 중 일부가 여러 부분으로 구성된 양식 데이터를 함께 전달하고 있음을 알 수있었습니다. 그러나 XML과 이미지를 함께 보내는 예제를 찾을 수 없었습니다.

다음은 내가 시도하는 코드입니다.

<?php 
$url = "http://www.myservice.com/event"; 

$file = dirname(__FILE__) . "\image.jpg"; 
$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10); 

$event_xml_data = 
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . 
'<ns5:event ' . 
'xmlns="http://www.myservice.com/Common/"' . 
'xmlns:ns2="http://www.myservice.com/Event/"' . 
'xmlns:ns3="http://www.myservice.com/Media/"' . 
'xmlns:ns4="http://www.myservice.com/Media/" ' . 
'xmlns:ns5="http://www.myservice.com/Event/"> ' . 
'<id>0</id>' . 
'<name>Event2004</name> '. 
'<ns2:description>Description845</ns2:description>' . 
'<ns2:startDate>2012-02-27T22:21:29.458-06:00</ns2:startDate>' . 
'<ns2:owner>Madhu</ns2:owner>' . 
'</ns5:event>'; 

$content_type_header = 'Content-Type: multipart/form-data; ' 
    .'type="application/xml"; ' 
    .'boundary="' . $boundary . '"; ' 
    .'start="<event>"; ' 
    .'start-info="application/xml";'; 
$accept_header = 'Accept: multipart/form-data'; 
$transfer_encoding_header = 'Transfer-Encoding: chunked'; 

$fileContents = file_get_contents($file); 

$headers = array($content_type_header, $accept_header, $transfer_encoding_header); 

$postData = $boundary . "\r\n" 
     ."Content-Type: application/xml\r\n\r\n" 
    ."Content-Transfer-Encoding: binary\r\n\r\n" 
     ."Content-ID: <event>\r\n\r\n" 
     .$event_xml_data . "\r\n" 
     .$boundary . "\r\n" 
     ."Content-Type: image/jpeg\r\n" 
     ."Content-Transfer-Encoding: binary\r\n\r\n" 
     . $fileContents . "\r\n" 
     .$boundary . "--\r\n"; 

$postDataNoMedia = $boundary . "\r\n" 
     ."Content-Type: application/xml\r\n\r\n" 
    ."Content-Transfer-Encoding: binary\r\n\r\n" 
     ."Content-ID: <event>\r\n\r\n" 
     .$event_xml_data . "\r\n" 
     .$boundary . "--\r\n"; 

$f = fopen('request.txt', 'w'); 
//open connection 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_USERPWD, 'madhukampurath:xxxxxx'); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$postDataNoMedia); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HEADER,1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); 
curl_setopt($ch, CURLOPT_VERBOSE ,1); 
curl_setopt($ch, CURLOPT_STDERR , $f); 

//execute post 
$output = curl_exec($ch); 


$error = curl_error($ch); 
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE); 

fclose($f); 

echo $http_code . "<br />"; 

echo $output; 

echo $error; 

curl_close($ch); 

?> 

웹 서비스는 java에서 수행됩니다. Tomcat 로그가이 오류를 표시합니다.

 
org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse 
WARNING: WebApplicationException has been caught : Couldn't find MIME boundary: -----------------------8a09e81751 

답변

0

내 친구 제럴드 캔터의 도움으로 문제를 해결할 수 있습니다. 나는 아래 답변을 게시하고 있습니다.

요청이 잘못 되었기 때문입니다. 경계가 올바르게 정의되지 않았습니다. 변경된 섹션은 다음과 같습니다.

<?php 
$url = "http://www.myservice.com/event"; 

$file = dirname(__FILE__) . "\image.jpg"; 
$boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10); 

$event_xml_data = 
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . 
'<ns5:event ' . 
'xmlns="http://www.myservice.com/Common/"' . 
'xmlns:ns2="http://www.myservice.com/Event/"' . 
'xmlns:ns3="http://www.myservice.com/Media/"' . 
'xmlns:ns4="http://www.myservice.com/Media/" ' . 
'xmlns:ns5="http://www.myservice.com/Event/"> ' . 
'<id>0</id>' . 
'<name>Event2004</name> '. 
'<ns2:description>Description845</ns2:description>' . 
'<ns2:startDate>2012-02-27T22:21:29.458-06:00</ns2:startDate>' . 
'<ns2:owner>Madhu</ns2:owner>' . 
'</ns5:event>'; 

$content_type_header = 'Content-Type: multipart/form-data; ' 
    .'type="application/xml"; ' 
    .'boundary="' . $boundary . '"; ' 
    .'start="<event>"; ' 
    .'start-info="application/xml";'; 
$accept_header = 'Accept: multipart/form-data'; 
$transfer_encoding_header = 'Transfer-Encoding: chunked'; 

$fileContents = file_get_contents($file); 

$headers = array($content_type_header, $accept_header, $transfer_encoding_header); 

// Here is the change. 
$postData = "--" . $boundary . "\r\n" 
      ."Content-Type: application/xml\r\n" 
      ."Content-Transfer-Encoding: binary\r\n" 
      ."Content-ID: <event>\r\n\r\n" 
      .$event_xml_data . "\r\n" 
      ."--" .$boundary . "\r\n" 
      ."Content-Type: image/jpeg\r\n" 
      ."Content-Transfer-Encoding: binary\r\n" 
      ."Content-ID: <media>\r\n\r\n" 
      . $fileContents . "\r\n" 
      ."--" .$boundary . "--"; 

$postDataNoMedia = "--" . $boundary . "\r\n" 
      ."Content-Type: application/xml\r\n" 
      ."Content-Transfer-Encoding: binary\r\n" 
      ."Content-ID: <event>\r\n\r\n" 
      .$event_xml_data . "\r\n" 
      ."--" .$boundary . "--"; 
// Change ends. 

$f = fopen('request.txt', 'w'); 
//open connection 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_USERPWD, 'madhukampurath:xxxxxx'); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$postDataNoMedia); 
curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt($ch, CURLOPT_HEADER,1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); 
curl_setopt($ch, CURLOPT_VERBOSE ,1); 
curl_setopt($ch, CURLOPT_STDERR , $f); 

//execute post 
$output = curl_exec($ch); 


$error = curl_error($ch); 
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE); 

fclose($f); 

echo $http_code . "<br />"; 

echo $output; 

echo $error; 

curl_close($ch); 

?>