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

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条)

报告相同问题?

悬赏问题

  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码
  • ¥15 编写vba代码实现数据录入工作