dongping5230 2018-03-23 14:19
浏览 715
已采纳

PHP cURL文件获取内容始终为空

I've created an endpoint in Paw (and/or Postman) for handling file uploads. It works great!

enter image description here

It's pretty simple. And in my application I can do something like:

echo file_get_contents('php://input');

And it will print out an encoded representation of the image/file (a cute kitten).

The problem is, I cannot seem to reproduce this behaviour using just cURL (Current application uses Guzzle).

When I try something like:

$target_url = 'http://my-document-handling-service.net:8000/documents';

$request = curl_init($target_url);

curl_setopt($request, CURLOPT_POST, true);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_POSTREDIR, 3);

curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '/Users/xxxxxxx-xxxx/Documents/kitten-test-upload-image-1.jpg'
    ));

echo curl_exec($request);

curl_close($request);

The body (file_get_contents()) is always empty. I don't really understand cURL I've always just used Guzzle - but now I have to use cURL and I can't get the body.

What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • dongzhao2725 2018-03-23 15:04
    关注

    (too long for a comment, the existing answer is very close to being correct, albeit bugged and won't work, but doesn't say why your origianl code failed) - when you give CURLOPT_POSTFIELDS an array, it encodes the input in multipart/form-data-format, and PHP parses multipart/form-data into $_POST (and for file uploads, $_FILES), and empties php://input in the process (it does this for any encoding it has built-in support for, and as of writing, there are 2, application/x-www-form-urlencoded, andmultipart/form-data`), and your code

    curl_setopt(
        $request,
        CURLOPT_POSTFIELDS,
        array(
          'file' => '/Users/xxxxxxx-xxxx/Documents/kitten-test-upload-image-1.jpg'
        ));
    

    sends the variable file with the value '/Users/xxxxxxx-xxxx/Documents/kitten-test-upload-image-1.jpg' to the server in the multipart/form-data format, thus your value is actually in $_POST['file'], but if you want to send the raw image directly, no encoding, so you can use php://input on the target server, use CURLOPT_INFILE or CURLOPT_POSTFIELDS, for example

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_INFILE,($fp=fopen('abc.txt','rb')));
    curl_setopt($ch, CURLOPT_INFILESIZE,123);
    (...)
    curl_exec($ch);
    fclose($fp);
    

    or

    curl_setopt($ch,CURLOPT_POSTFIELDS,file_get_contents("abc.txt"));
    curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/octet-stream'));
    curl_exec($ch);
    

    (note that the second method will use more ram, it will put the entire file in memory prior to uploading, while the first method will send the data in chunks, making it possible to upload any size files without running out of ram, unlike the second method)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程