php - 如何在没有内存开销的情况下在 PHP curl 中 POST 大量数据?

标签 php post curl

我目前正在使用 PHP curl 扩展与一些 HTTP API 进行通信。

我使用批量加载器一次执行大量操作。必须使用 POST 方法调用批量端点,因此我使用如下代码:

<?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 能够使用足够的内存...

我需要将 memory_limit 设置为一个高值,只是为了批量加载...

有没有办法使用文件流发送带有 curl PHP 扩展的数据?我看到了 CURLOPT_INFILECURLOPT_READFUNCTION 但它似乎不适用于 POST 方法...

我还看到 curl 命令行工具能够执行 --data "@/path/to/file/content" 这似乎是我需要的...

有什么想法吗?

最佳答案

使用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);

关于php - 如何在没有内存开销的情况下在 PHP curl 中 POST 大量数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32627346/

相关文章:

curl - 无法解析代理 : POST (while running curl script for watson document conversion)

c++ - 从 C++ 运行 CURL 以异步获取多个 URL

php - 在 php 的 stackoverflow 上有像这里这样的成就系统吗?

php - 在本地 Laravel Homestead 服务器上验证自签名证书

javascript - Jquery自动完成源php函数

javascript - 溢流体不起作用?

php - 我想将数据插入我的数据库,但它不起作用

javascript - POST 请求中 Html 丢失

python - 性感:如何在 HTTP POST 中验证来自 json 数据的非字符串值

php - Guzzle (35) gnutls_handshake() 失败 : An unexpected TLS packet was received over HTTP