简体   繁体   English

使用 curl 在 postfields 上传带有文件@的 POST 数据

[英]Using curl to upload POST data with files @ in postfields

I would like to use cURL to not only send data parameters in HTTP POST but to also upload files with specific name.我想使用 cURL 不仅可以在 HTTP POST 中发送数据参数,还可以上传具有特定名称的文件。 How should I go about doing that?我应该怎么做 go 一下?

the api also gives example of how to do the curl command 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

so this is the function i have所以这是我的 function

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

the response i get is {"status":"error","message":"No file inputted"}我得到的响应是 {"status":"error","message":"No file inputed"}

Update: fixed by replacing string @beztam.mp4 to curl_file_create(__DIR__.'/beztam.mp4')更新:通过将字符串 @beztam.mp4 替换为curl_file_create(__DIR__.'/beztam.mp4')来修复

You can use:您可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM