douxing5598 2012-02-29 15:21
浏览 112
已采纳

php将multipart和xml一起提交给Web服务

I need to submit an XML and an image together to a restful web service. Is this possible using curl ?

Of the various examples available in web, I could see that they are passing a multipart and other form data together. But, I could not locate an example which sends an XML and an image together.

Here is the code I am trying.

<?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 . "
"
        ."Content-Type: application/xml

"
     ."Content-Transfer-Encoding: binary

"
        ."Content-ID: <event>

"
        .$event_xml_data . "
"
        .$boundary . "
"
        ."Content-Type: image/jpeg
"
        ."Content-Transfer-Encoding: binary

"
        . $fileContents . "
"
        .$boundary . "--
";

$postDataNoMedia  = $boundary . "
"
        ."Content-Type: application/xml

"
     ."Content-Transfer-Encoding: binary

"
        ."Content-ID: <event>

"
        .$event_xml_data . "
"
        .$boundary . "--
";

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

?>

Web service is done in java. Tomcat logs give this error.

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

  • 写回答

1条回答 默认 最新

  • duanqilupinf67040 2012-03-14 09:26
    关注

    I could solve the issue with the help of my friend Gerald Cantor. I am posting the answer below.

    It was because of a badly formed request. The boundaries were not defined correctly. Changed section is indicated below.

    <?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 . "
    "
                ."Content-Type: application/xml
    "
                ."Content-Transfer-Encoding: binary
    "
                ."Content-ID: <event>
    
    "
                .$event_xml_data . "
    "
                ."--" .$boundary . "
    "
                ."Content-Type: image/jpeg
    "
                ."Content-Transfer-Encoding: binary
    "
                ."Content-ID: <media>
    
    "
                . $fileContents . "
    "
                ."--" .$boundary . "--";
    
    $postDataNoMedia  = "--" . $boundary . "
    "
                ."Content-Type: application/xml
    "
                ."Content-Transfer-Encoding: binary
    "
                ."Content-ID: <event>
    
    "
                .$event_xml_data . "
    "
                ."--" .$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); 
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码