【问题标题】:Send file over cURL - php通过 cURL 发送文件 - php
【发布时间】:2018-11-09 22:35:54
【问题描述】:

我需要通过 cURL 将文件发送到服务器,服务器需要文件中的以下内容:

  • 文件名
  • 校验和
  • mimeType
  • 尺寸

我尝试了以下方法:

  1. 使用 @ 和文件的完整路径
  2. 文件获取内容
  3. 只是完整路径
  4. 新的 CURLFile,带和不带额外参数

    $mime = mime_content_type($file); 
    $info = pathinfo($file); 
    $name = $info['basename']; 
    $output = new CURLFile($file, $mime, $name);
    
  5. curl_file_create

  6. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);

当我使用@ 时,它只是要求提供上述额外的细节。当我使用 new CURLFile 和 curl_file_create 时,浏览器会超时。

但是当我直接在 bash 中使用时,像这样:

curl -X POST -F 'file=@dog.jpg' -F 'category=video' -F 'language=English' -F 
'subject_name= Accounting' -F 'studyYear=ALL' -F 'title=Test resources 3' -F 
'description=This is a test 3' http://127.0.0.1:5000/postfile -u root -p

然后它完美地工作。我不确定我应该在 php 中做什么。

编辑

这是我在 cURL 到服务器时遇到的错误:

{"query":"add a new resource","doc":"resource-2db43d6f","info":"Schema validation failed","_xerror":true,"message":"child \"filename\" fails because [\"filename\" is required]. child \"checkSum\" fails because [\"checkSum\" is required]. child \"mimeType\" fails because [\"mimeType\" is required]. child \"size\" fails because [\"size\" is required]","isJoi":true,"name":"ValidationError","details":[{"message":"\"filename\" is required","path":"filename","type":"any.required","context":{"key":"filename"}},{"message":"\"checkSum\" is required","path":"checkSum","type":"any.required","context":{"key":"checkSum"}},{"message":"\"mimeType\" is required","path":"mimeType","type":"any.required","context":{"key":"mimeType"}},{"message":"\"size\" is required","path":"size","type":"any.required","context":{"key":"size"}}],"_object":{"_id":"resource-2db43d6f","owner":1,"created":1541769732,"private":false,"feedback":false,"category":"video","language":"English","studyYear":"ALL","title":"testing code","description":"This is a test","subject":0},"reqPath":"POST /api/1.0/resources/","detail":"child \"filename\" fails because [\"filename\" is required]. child \"checkSum\" fails because [\"checkSum\" is required]. child \"mimeType\" fails because [\"mimeType\" is required]. child \"size\" fails because [\"size\" is required]","person":"user"}`

所以这些字段必须包含在文件本身中。而不是额外的。

【问题讨论】:

  • 请解释一下“校验和”是什么意思。您显示的 cURL 命令似乎没有在这方面执行任何额外的计算,因此您的实际意思尚不清楚。
  • 好吧,在 bash 中使用它时,我什至不必指定文件的 mimeType 和大小。但是当我使用 php 执行普通 cURL 时,服务器返回我需要包含这些参数。所以校验和我什至不确定,但错误表明它是必需的。
  • 这仍然没有解释校验和应该是什么。您是否有权访问此服务器,以便您可以记录在这两种情况下它接收到的确切内容?否则,请设置您自己的脚本来执行此操作,然后以两种方式向它发送请求(cURL CLI,您尝试使用 PHP),并检查相关差异可能是什么。
  • 检查我上面的编辑

标签: php


【解决方案1】:

我发现这个示例在 Google 中进行了快速搜索。

用于搜索的关键字是:在 php 中通过 cURL 发送文件

以下代码向http://example.com/发出请求,上传 文件 example.txt,并输出响应。

// initialise the curl request
$request = curl_init('http://example.com/');

// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
    $request,
    CURLOPT_POSTFIELDS,
    array(
      'file' => '@' . realpath('example.txt')
    ));

// output the response
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);

// close the session
curl_close($request);

学分:

凯特·莫利 http://code.iamkate.com/php/sending-files-using-curl/

【讨论】:

  • 也这样做了。在我的列表中排名第一。
猜你喜欢
  • 2015-01-29
  • 2012-08-15
  • 2017-04-23
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
  • 1970-01-01
  • 2022-11-29
相关资源
最近更新 更多