PHP:设置MIME消息的内容长度

PHP:设置MIME消息的内容长度,php,mime,Php,Mime,我必须用php构建一个API,将请求返回给Java编码器。 这就是我所做的 header("MIME-Version: 1.0".$eol); header("Connection: Keep-Alive".$eol); header("Accept-Encoding: gzip, deflate".$eol); header("Host: host".$eol.$eol); header("Content-Type: multipart/related; boundary...//not su

我必须用php构建一个API,将请求返回给Java编码器。 这就是我所做的

header("MIME-Version: 1.0".$eol);
header("Connection: Keep-Alive".$eol);
header("Accept-Encoding: gzip, deflate".$eol);
header("Host: host".$eol.$eol);
header("Content-Type: multipart/related; boundary...//not sure if I can show boundary".$eol);
echo $res;
因此,有主标题和
$res
变量,它是响应的bdy。 我是这样形成的

$eol="\r\n";
$BOUNDARY = "boundary...//not sure if I can show boundary"; 
$res="--".$BOUNDARY.$eol;
$res .= "Content-Type: application/json".$eol;
ob_start();
print_r($json);
$result = ob_get_clean();
$data .= "Content-Length: ".strlen($result).$eol.$eol;
$res.=$result;
$res.=$eol.$eol;
$res.="--".$BOUNDARY.$eol;
ob_start();
readfile('/var/www/9292/inputPhoto.jpg');
$result = ob_get_clean();
$res.="Content-Type: image/jpeg".$eol;
$imageLength=strlen($result)+6;
//I add +6 because of 3 $eol
$res.=$eol;
$res.=$result;
$res.=$eol.$eol;
$res.="--".$BOUNDARY.$eol;
$res .= "Content-Type: application/octet-stream".$eol;
ob_start();
readfile('/var/www/9292/1.etmpl');
$result = ob_get_clean();   
$vectorLength=strlen($result)+6;
$res .= "Content-Length: ".$vectorLength.$eol;
$res.=$eol;
$res.=$result;
$res .= $eol.$eol;
$res .= "--".$BOUNDARY."--".$eol;
下面是Java编码器收到的输出

HTTP/1.1 200 OK
Date: Wed, 22 Apr 2015 12:30:47 GMT
Server: Apache/2.4.10 (Debian)
MIME-Version: 1.0
Connection: Keep-Alive, Keep-Alive
Accept-Encoding: gzip, deflate
Host: 99.999.999.99:99
Keep-Alive: timeout=5, max=100
Transfer-Encoding: chunked
Content-Type: multipart/related; boundary=--boundary...//not sure if I can show boundary

2d66
--boundary...//not sure if I can show boundary
Content-Type: application/json
{JSON That I can't show here}

--boundary...//not sure if I can show boundary
Content-Type: image/jpeg
Content-Length: 1917

����JFIF``��C
*��^I _|�rd�HE�8�H X}�)o�d�����ƭ�2I�u�I P�$��
Content of picture
----1429705906426boundary--


--boundary...//not sure if I can show boundary
Content-Type: application/octet-stream
Content-Length: 9222

H67a831e8-5f0
Content of vector
--boundary...//not sure if I can show boundary
奇怪的是,我没有JSON的内容长度,但我将其设置为您可以在代码中看到的长度。我还可以在第一个边界之前看到一些奇怪的
2d66
。 否则输出很好,Java编码器可以接收图像、向量和json。 但他也犯了一个错误:

Unexpected end of headers detected. Higher level boundary detected or EOF reached.

据我所知,内容长度在某些地方是错误的。任何有MIME经验的人都可以告诉我错误吗?

为什么
java
被标记为这个问题?好的,我删除了它。我不确定它是否会产生某种影响。