php curl提交post请求到一个网址 如何获取返回的文件名
发送curl 请求的时候加上
// 获取响应的消息头 curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); // 解析Http头的Content-Disposition获取文件名 $tmpInfo = curl_exec($curl); list($headers, $body) = explode("\r\n\r\n", $tmpInfo, 2); $header_array = explode("\n", $headers[0]); foreach($header_array as $header_value) { $header_pieces = explode(':', $header_value); if(count($header_pieces) == 2) { $headers[$header_pieces[0]] = trim($header_pieces[1]); } } $file_name = $headers['Content-Disposition']; $file_type = $headers['Content-Type']; $file_content = $body;
可以参考以上代码,如果没有Content-Disposition,需要特殊处理下,可以看看对方返回的文件名放在哪。
保存文件只需要把 $file_content 写入打开的文件句柄