2 回答
TA貢獻1788條經驗 獲得超4個贊
你解決問題了嗎?下面的簡單代碼對我有用
$fp = fopen('/your path where you store the zip file/'.$filename, 'w+');
if ($fp == FALSE){
print "File not opened<br>";
exit;
}
fwrite($fp, $response);
fclose($fp);
$response是 API 響應的正文,它將是 $filename從標頭中獲取的不可讀格式的 zip 文件名。
TA貢獻1735條經驗 獲得超5個贊
似乎 Json Encoded 并且您必須在 php 腳本中使用 json 對其進行解碼,因此: json_decode(string,array) string = 您收集的編碼響應。array = True 如果你想要結果數據的數組。
2- 使用 header('Content-Type: application/json') (此標頭在發送或接收響應之前最有用)
3 -錯誤處理:json_last_error() json_last_error_msg()
try {
$header = $resultInfo->getHeader('Content-Disposition');
if (!empty($header)) {
if (strpos($header, 'filename') !== false) {
$filename = substr($header, strpos($header, 'filename'));
$str = explode('=', $filename);
$body = $resultInfo->getBody();
$fp = fopen(storage_path("csv/{$str[1]}"), 'w');
##uncomment below line if response not valid may help you.
# header('Content-Type: application/json');
$dbody = json_decode($body,true);
fwrite($fp, $dbody);
fclose($fp);
}
}
} catch (\Exception $e) {
echo $e->getMessage();
}
希望能有所幫助
- 2 回答
- 0 關注
- 143 瀏覽
添加回答
舉報
