需求背景:最近在做一个项目,用户下载文件
后端用的是LARAVEL框架,先贴出代码:
return (new StreamedResponse(function() use ($id, $model) {
// $handle = fopen('php://output', 'w');
$ch = curl_init();
// $callback = function ($ch, $str) use ($handle) {
// $length = fwrite($handle, $str);
// fflush($handle);
// return $length;
// };
curl_setopt($ch, CURLOPT_URL, getFileUrlBySignId($id));
curl_setopt($ch, CURLOPT_HEADER, 0);
if (is_dev_net()) {
curl_setopt($ch, CURLOPT_PROXY, "http://web-proxy.tencent.com"); //代理服务器地址
curl_setopt($ch, CURLOPT_PROXYPORT, 8080); //代理服务器端口
}
// curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback);
$verbose = fopen('/mnt/windows/cstudyplus/qqq.txt', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);
// fclose($handle);
}, 200, $headers));
一开始我们CURL的机制不了解,以为对拉取文件的CURL过程是 , 先获取全部的内容,然后在把内容输出到PHP的标准输出,这样的话,以为会很占当前PHP的内存。 然后当看了下源码的时候,并不是这样的
真实情况, CURL 每次读取SOCKET包数据的时候 ,每次获取到达1348个字节的时候(不知道是不是MTU单位的限制)会强制默认输出给PHP标准输出,PHP自己又有输出缓存区,默认是4KB,当缓冲区满的时候,就会通过TCP发送给浏览器,所以PHP脚本执行的过程中,大文件小文件下载的过程中,不会占用很多内存。
所以这个CURL过程是拉取一定字节,就会输出给PHP标准输出, 下面贴出具体代码
curl_write 是CURL每次获取包数据的时候,回调的函数,用于处理每次包的数据, 如果没有重定向输出的话 默认PHP_CURL_STDOUT 输出到标准输出
static size_t curl_write(char *data, size_t size, size_t nmemb, void *ctx)
{
php_curl *ch = (php_curl *) ctx;
php_curl_write *t = ch->handlers->write;
size_t length = size * nmemb;
#if PHP_CURL_DEBUG
fprintf(stderr, "curl_write() called\n");
fprintf(stderr, "data = %s, size = %d, nmemb = %d, ctx = %x\n", data, size, nmemb, ctx);
#endif
switch (t->method) {
case PHP_CURL_STDOUT:
PHPWRITE(data, length);
break;
case PHP_CURL_FILE:
return fwrite(data, size, nmemb, t->fp);
case PHP_CURL_RETURN:
if (length > 0) {
smart_str_appendl(&t->buf, data, (int) length);
}
break;
case PHP_CURL_USER: {
zval argv[2];
zval retval;
int error;
zend_fcall_info fci;
GC_ADDREF(ch->res);
ZVAL_RES(&argv[0], ch->res);
ZVAL_STRINGL(&argv[1], data, length);
fci.size = sizeof(fci);
fci.object = NULL;
ZVAL_COPY_VALUE(&fci.function_name, &t->func_name);
fci.retval = &retval;
fci.param_count = 2;
fci.params = argv;
fci.no_separation = 0;
ch->in_callback = 1;
error = zend_call_function(&fci, &t->fci_cache);
ch->in_callback = 0;
if (error == FAILURE) {
php_error_docref(NULL, E_WARNING, "Could not call the CURLOPT_WRITEFUNCTION");
length = -1;
} else if (!Z_ISUNDEF(retval)) {
_php_curl_verify_handlers(ch, 1);
length = zval_get_long(&retval);
}
zval_ptr_dtor(&argv[0]);
zval_ptr_dtor(&argv[1]);
break;
}
}
return length;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#define POSTURL "https://segmentfault.com/q/1010000006843493"
#define FILENAME "/mnt/windows/cstudyplus/ttt.txt"
size_t write_data(void* buffer,size_t size,size_t nmemb,void *stream)
{
FILE *fptr = (FILE*)stream;
fwrite(buffer, size, nmemb, fptr);
fprintf(stderr, "write_data() called\n");
fprintf(stderr, "size = %d, nmemb = %d, ctx = %x\n", size, nmemb, stream);
return size*nmemb;
}
int main(int argc,char *argv[])
{
CURL *curl;
CURLcode res;
FILE* fptr;
struct curl_slist *http_header = NULL;
if ((fptr = fopen(FILENAME, "w")) == NULL)
{
fprintf(stderr,"fopen file error:%s\n",FILENAME);
return -1;
}
curl = curl_easy_init();
if (!curl)
{
fprintf(stderr,"curl init failed\n");
return -1;
}
curl_easy_setopt(curl,CURLOPT_URL,POSTURL); //url地址
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, write_data); //对返回的数据进行操作的函数地址
curl_easy_setopt(curl,CURLOPT_WRITEDATA, fptr); //这是write_data的第四个参数值
res = curl_easy_perform(curl);
if (res != CURLE_OK)
{
switch(res)
{
case CURLE_UNSUPPORTED_PROTOCOL:
fprintf(stderr,"不支持的协议,由URL的头部指定\n");
case CURLE_COULDNT_CONNECT:
fprintf(stderr,"不能连接到remote主机或者代理\n");
case CURLE_HTTP_RETURNED_ERROR:
fprintf(stderr,"http返回错误\n");
case CURLE_READ_ERROR:
fprintf(stderr,"读本地文件错误\n");
default:
fprintf(stderr,"返回值:%d\n",res);
}
return -1;
}
curl_easy_cleanup(curl);
}
上面的curl_write就是PHP默认的输出回调函数,DEMO里面的write_dataj就是curl_write,如果有定义会覆盖自带的handler
验证是否是每次都会直接输出,而不是全部获取下来再一次性输出
, size = 1, nmemb = 547, ctx = 1668010
[root@localhost cstudyplus]# gcc -l curl -o curl curl.c
[root@localhost cstudyplus]# ./curl
write_data() called
size = 1, nmemb = 996, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
write_data() called
size = 1, nmemb = 1348, ctx = 1796010
至此,默认情况下就已经解决了这个问题,所以一开始我们就多虑了。