2017-12-06 16 views
0

私はバイナリデータを返す外部サービスにAPIを書いています。今、データを正しく表示するためのPDFを作成したいと思います。 APIのPHPバイナリデータをpdf

レスポンスヘッダ:

0 => string 'HTTP/1.1 200 OK' (length=15) 
1 => string 'Cache-Control: private' (length=22) 
2 => string 'Content-Length: 275454' (length=22) 
3 => string 'Content-Type: application/pdf' (length=29) 
4 => string 'Server: Microsoft-IIS/8.0' (length=25) 
5 => string 'model: Invariant' (length=16) 
6 => string 'Content-Disposition: attachment; filename=response_3a293a37-7a00-49ca-b557-fa218f834eb4.pdf' (length=91) 
7 => string 'X-AspNet-Version: 4.0.30319' (length=27) 
8 => string 'X-Powered-By: ASP.NET' (length=21) 
9 => string 'Set-Cookie: ARRAffinity=3d8fa41aef05715eddd28bd82d74a76f49904e3d3e55e9dbdf656e2598101182;Path=/;HttpOnly;Domain=x2c-test.sprengnetter.at' (length=136) 
10 => string 'Date: Wed, 06 Dec 2017 12:12:56 GMT' (length=35) 
11 => string 'Connection: close' (length=17) 

PDFの名前はランダムな名前です。私はバイナリデータとどのように作成すると多くの経験を持っていけない

%PDF-1.5 4 0 obj <>/XObject<>>>/Group <>>> endobj 5 0 obj <>stream x��Y[O#7�+~�Uc|�<���UUU[�ݪo�IJ�I�H�ԟ� 

:今、Imは私の応答のうちのPDFファイルを取得しようと

:対応して

$response_pdf = file_get_contents($remote_print, true, $context); 
    if($response_pdf === FALSE){ 
     var_dump($http_response_header); 
     echo "error pdf"; 
    }else{ 
     var_dump($http_response_header); 

     $base64 = "/$response_pdf/"; 
     $binary = base64_decode($base64); 
     file_put_contents('my.pdf',$binary); 
     header('Content-type: application/pdf'); 
     header('Content-Disposition: attachment; filename="my.pdf"'); 
     return $binary; 

はこのようなデータであり、バイナリデータのpdfだから私はいくつかの助けをしてうれしいです。

+0

[bin2hex](http://php.net/manual/ro/function.bin2hex.php)私はあなたが探しているものだと思います。 – Andrew

答えて

0

自分で自分の質問に答えることができました。

私はtempnamを使って一時ファイルを作成し、それをrename()で名前を変更して正しいパスを与えました。次に、生成した新しいファイルに対してfile_put_contents()を実行しました。