jsonで送信して、マルチパートレスポンスを受け取る

必要があって、検証したのでメモ。
マルチパートレスポンスするPG。
mime.php

<?php
  header("Content-type: multipart/mixed;boundary=boundaryString");
?>
--boundaryString
Content-Type: text/html; charset=UTF-8

  <html>
  <body>
    <p>複数フィアルのダウンロード</p>
  </body>
  </html>
  
--boundaryString
Content-Type:text/plain; charset=UTF-8
Content-Disposition: attachment; filename=test.txt

 data1,data2,data3
  
--boundaryString
Content-Type:image/jpg; charset=UTF-8
Content-Disposition: attachment; filename=test.jpg

<?php print file_get_contents("/path/to/file.jpg"); ?>

--boundaryString--

これをFirefoxから叩くと、複数の添付ファイルがダウンロード可能。
これをphpから取得、解析するPG
decode.php

<?php
require_once("Mail/mimeDecode.php");
//jsonでPOST送信
$curl = curl_init("http://path_to/mime.php");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '{"reserid":"' . 3 . '"}');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
//レスポンスヘッダーの取得
$info = curl_getinfo($curl);
curl_close($curl);

$data['include_bodies'] = true;
$data['input'] = "Content-type: ".$info['content_type']."\r\n\r\n".$res;
$res = Mail_mimeDecode::decode($data);
$ret    = array();
foreach( $res->parts as $obj ){
        $ret[] = $obj->body;
}
file_put_contents("ni.jpg", $ret[2]);
var_dump( $ret );
exit;