【问题标题】:Using curl to upload POST data with files @ in postfields使用 curl 在 postfields 中上传带有文件 @ 的 POST 数据
【发布时间】:2021-01-30 07:04:15
【问题描述】:

我想使用 cURL 不仅可以在 HTTP POST 中发送数据参数,还可以上传具有特定名称的文件。我该怎么做呢?

该 api 还提供了如何执行 curl 命令的示例

curl -k -F file=@/path/to/file.txt -F apiId={apiId} -F apiSecretId={apiSecretId} https:\/\/fa2a.ninjastream.to\/api\/file\/upload?expires=1596028934&signature=132d1332996262809c7cc2dcd6376c483d6521dc7c273f9275ee1acb53e0b072

这就是我的功能

function uploadfileandgetlink($uploadurl){
  
          $ch = curl_init();

          curl_setopt($ch, CURLOPT_URL, $uploadurl);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  
  
 
          $post = array(
            
            
              'file'  => '@beztam.mp4',
              'apiId' => 'sdfg',
              'apiSecretId' => 'dsfg'
          );
          curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

          echo $result = curl_exec($ch);
          if (curl_errno($ch)) {
              echo 'Error:' . curl_error($ch);
          }
          curl_close($ch);

我得到的响应是 {"status":"error","message":"No file input"}

更新:通过将字符串 @beztam.mp4 替换为 curl_file_create(__DIR__ .'/beztam.mp4')

【问题讨论】:

  • 你看过CURLFile吗?
  • @ProfessorAbronsius 非常感谢我试过它就像一个魅力
  • 太棒了!很高兴这有效 - 祝你项目的其余部分好运
  • 太棒了!很高兴这有效 - 祝你项目的其余部分好运

标签: php curl post file-upload


【解决方案1】:

你可以使用:

$fileContent = file_get_contents('beztam.mp4'); 
$post = array(
            
            
              'file'  => rawurlencode($fileContent),
              'apiId' => 'sdfg',
              'apiSecretId' => 'dsfg'
          );

【讨论】:

  • 没用,但我发布了解决方案,非常感谢您花时间提供帮助
猜你喜欢
  • 2012-09-21
  • 2011-03-28
  • 1970-01-01
  • 2018-07-19
  • 2015-01-02
  • 1970-01-01
  • 2021-05-08
  • 1970-01-01
  • 2019-07-10
相关资源
最近更新 更多