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

不打扰是莪最后的温柔 提交于 2019-12-08 10:30:13

问题


My Goal : I want to make certificate and download it .

I have the following scenario :

in outlines :

request : browser ---> PHP page ---> PHP Rest web service ---> bash

response : bash ---> PHP Rest web service ---> PHP page ---> browser

in details :

  1. REst web service called from php page does next :
    • makes certificate in bash and exports it as pfx file .
    • returns certificate from bash file as byte stream . hexdump -b $exportedcert
  2. returned output from web service is bytes (this is just a part of it ):

    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. here when I'm stacked !! I have to download this stream after converting it back to binary . I've tried many many codes , and I'm not getting results .

      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!";
    }
    

certificate is corrupted or invalid when adding it to browser .

any suggestions ?
P.S : I'm using Restler & Pest libraries .


回答1:


I had figured this out .

first , upgrade php to 5.4.* to use hex2bin and notice that hex2bin input parameters doesn't have any spaces in it .

second , use xxd with option -p to get hex output with no offset and then remove spaces from piece of output .

that would work perfectly .



来源:https://stackoverflow.com/questions/17145651/download-binary-stream-data-from-browser-using-php-with-web-service-downloaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!