如何在没有内存开销的情况下在PHP curl中发布大量数据?
我目前正在使用PHP curl扩展程序与某些HTTP API通信.
I'm currently using PHP curl extension to communicate with some HTTP APIs.
我使用批量加载器一次执行很多操作.必须使用POST方法调用批量端点,因此我使用的代码如下:
I use a bulk loader to perform a lot of operations at once. The bulk endpoint must be called with POST method so I use a code like :
<?php
$h = curl_init($url);
curl_setopt(CURLOPT_POST, true);
curl_setopt(CURLOPT_POSTFIELDS, $data);
curl_setopt(CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($h);
curl_close($h);
批量端点允许我发送大量数据(一次超过200Mo).目前,我需要将数据加载到一个变量中,该变量要求PHP能够使用足够的内存...
The bulk endpoint allows me to send a huge amount of data (more than 200Mo at once). For the moment I need to load the data inside a variable which require PHP to be able to use enough memory...
我只需要为批量加载将memory_limit
设置为较高的值...
I need to set memory_limit
to a high value just for the bulk load...
有没有一种方法可以使用文件流来发送带有curl PHP扩展名的数据?我看到了CURLOPT_INFILE
和CURLOPT_READFUNCTION
,但似乎不适用于POST方法...
Is there a way to use a file stream to send data with the curl PHP extension ? I saw the CURLOPT_INFILE
and CURLOPT_READFUNCTION
but it seems to don't work with POST method...
我还看到curl命令行工具能够执行--data "@/path/to/file/content"
,这似乎正是我所需要的...
I also saw that the curl command line tool is able to perform a --data "@/path/to/file/content"
which seems to be what I need...
有什么想法吗?
使用CURLOPT_INFILE
$curl = curl_init();
curl_setopt( $curl, CURLOPT_PUT, 1 );
curl_setopt( $curl, CURLOPT_INFILESIZE, filesize($tmpFile) );
curl_setopt( $curl, CURLOPT_INFILE, ($in=fopen($tmpFile, 'r')) );
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'POST' );
curl_setopt( $curl, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json' ] );
curl_setopt( $curl, CURLOPT_URL, $url );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec($curl);
curl_close($curl);
fclose($in);