场景
- 客户的支付宝账单压缩包85MB, 加上进程其他的消耗 超过了php默认128MB的限制,导致下载失败
分析
目前方案使用curl请求文件地址, 将文件数据放入变量,然后通过fwrite写入磁盘; 变量太大超过了内存限制
所以需要考虑直接写入硬盘
function alipayCurl($sid, $url, $filePath)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$content = curl_exec($ch);
$errno = curl_errno($ch);
if ($errno) {
logx("alipay_query start down fail curl_errno" . print_r($errno, true), $sid);
curl_close($ch);
return false;
} else {
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode) {
logx("alipay_query start down fail curl_getinfo" . print_r($httpStatusCode, true), $sid);
curl_close($ch);
return false;
}
}
curl_close($ch);
$resource = fopen($filePath, 'w');
fwrite($resource, $content);
fclose($resource);
return true;
}
解决
function alipayCurlNew($sid, $url, $filePath)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
$fp = fopen ($filePath, 'w+');
curl_setopt($curl, CURLOPT_FILE, $fp);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl,CURLOPT_TIMEOUT,50);
curl_exec($curl);
if (curl_errno($curl)) {
$message = curl_error($curl);
logx("下载账单文件失败" . $message , $sid);
curl_close($curl);
return false;
}
curl_close($curl);
fclose($fp);
return true;
}
|