我必须在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经验的人都会向我显示错误吗?