2012-03-08 56 views
1

我一直無法找到使用PHP的解決方案。基本上,當用戶點擊「下載PDF」鏈接時,它將調用一個PHP函數,該函數需要一個字節數組並將其解析爲PDF。我不知道如何去做。任何幫助將是偉大的!將字節數組保存到文件PHP

編輯: 我得到的 「字節數組」 是這樣的:

<?php 
$userPDF = $_POST['username']; 
$passPDF = $_POST['password']; 
$idPDF = 'MO-N007175A'; 
//PDF Function 
$data = array("id" => $idPDF, "username" => $userPDF, "password" => $passPDF);                  
$data_string = json_encode($data);             
$ch = curl_init('http://*****.com/****/v1/DealPdf');                  
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                  
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                 
curl_setopt($ch, CURLOPT_VERBOSE, 1);                 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
    'Content-Type: application/json')                  
);                             
$result = curl_exec($ch); 
var_dump($result); 
?> 

的 「$結果」 是假想的字節數組,看起來像這樣:

string(1053240) "[37,80,68,70,45,49,46,54,10,37,211,244,204,225,10,49,32,48,32,111,98 

..等等 基本上我需要採取什麼即時回來並將其保存爲PDF(或任何通用文件)。

+2

而這個字節數組來源於......在哪裏? PHP中沒有像'byte array'這樣的東西。 – 2012-03-08 17:21:04

+0

它來自WCF服務,我從POST中檢索它。 – mdance 2012-03-08 17:31:10

+0

那麼......你想如何寫入對象到文件?作爲JSON字符串,我認爲? – 2012-03-08 17:35:25

回答

1

你可以嘗試這樣的事情:

// suppose that the response is a JSON array (look like it) 
$bytes = json_decode($results); 

$fp = fopen('myfile.pdf', 'wb+'); 

while(!empty($bytes)) { 
    $byte1 = array_shift($bytes); 
    $byte2 = array_shift($bytes); 
    if(!$byte2) { 
     $byte2 = 0; 
    } 

    fwrite($fp, pack("n*", ($byte1 << 8) + $byte2); // big endian byte order ? 
} 

fclose($fp);