使用带有web服务的PHP从浏览器下载二进制流数据;下载的文件已损坏


Download binary stream data from browser using PHP with web service " downloaded file is corrupted "

我的目标:我想制作证书并下载。

我有以下场景:

:

请求

: PHP浏览器-> PHP页面-> Rest web服务——> bash

反应

: bash——> PHP Rest web服务浏览器-> PHP页面->

detail:

  1. REst web服务从php页面调用做下一步:
    • 用bash生成证书,并导出为pfx文件。
    • 从bash文件返回证书作为字节流。hexdump -b $exportedcert
  2. 从web服务返回的输出是字节(这只是它的一部分):

    0000000 060 202 024 071 002 001 003 060 202 023 377 006 011 052 206 110 0000010 206 367 015 001 007 001 240 202 023 360 004 202 023 354 060 202 0000020 023 350 060 202 016 237 006 011 052 206 110 206 367 015 001 007 0000030 006 240 202 016 220 060 202 016 214 002 001 000 060 202 016 205 0000040 006 011 052 206 110 206 367 015 001 007 001 060 034 006 012 052 0000050 206 110 206 367 015 001 014 001 006 060 016 004 010 276 147 122 0000060 363 175 042 303 050 002 002 010 000 200 202 016 130 212 124 302 0000070 271 370 201 316 300 134 133 246 211 062 276 045 241 020 101 155 0000080 057 103 205 232 164 203 265 376 057 067 274 361 057 274 367 110 0000090 251 107 205 130 306 035 267 377 316 223 242 347 363 234 341 052 .....

  3. 这里当我堆叠!!我必须下载这个流转换回二进制后。我试了很多很多代码,但我没有得到结果。

      function hex2bin($data) {
            //function URL: http://fiddyp.co.uk/code-to-reverse-a-bin2hex-function/
            //function author: Andy Bailey
            $len = strlen($data);
            for($i=0;$i<$len;$i+=2) {
                $newdata .= pack("C",hexdec(substr($data,$i,2)));
            }
            return $newdata;
        }  
      $api_url = "http://mydomain/certificates";
      try {
       // @url POST /client_cert
         $result = $pest->post('/client_cert', $data);
         $res=explode('"', $result);
         $hex = hex2bin($res[1]);
         header("Content-Description: File Transfer");
         header("Content-Disposition: attachment; filename=hex.crt");
         header("Content-Type: application/octet-stream ");
         header("Content-Transfer-Encoding: binary");*
         // Read the file from disk
        echo $hex;
    } catch (Pest_NotFound $e) {
        // 404
        echo "Certificate with Name = " . $certName_pfx . " doesn't exist!";
    }
    

证书添加到浏览器时,证书已损坏或无效。

有什么建议吗?
附注:我正在使用Restler &害虫库。

我已经明白了。

首先,将php升级到5.4.*使用hex2bin,并注意hex2bin输入参数中没有任何空格。

第二,使用xxd和选项-p来获得十六进制输出,没有偏移,然后从输出块中删除空格。

可以很好地工作。