dsklzerpx64815631 2018-06-07 19:49 采纳率: 100%
浏览 153
已采纳

Curl POST上传文件请求返回false

I'm trying to use the gfycat API to create a gfycat with a file upload through curl with php but it doesn't work and var_dump($response) gives me bool(false).

My Code:

$file_path = $target_dir.$newfilename;
$cFile = curl_file_create($file_path);
$data = array(
     "file" => $cFile,
);
$target_url = "https://filedrop.gfycat.com";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
     "Content-Type: multipart/form-data"
));            

$response = curl_exec($ch);
var_dump($response); // bool(false) here

curl_close($ch);

Help would be really appreciated. Thanks.

  • 写回答

2条回答 默认 最新

  • dqajyxqem115006813 2018-06-13 11:21
    关注

    Btw I got it to work with this:

    $file_path = "ABSOLUTE_FILE_PATH".$newfilename;
    $cFile = curl_file_create(realpath($file_path));
    $data = array(
    "key" => $newfilename,
    "file" => $cFile,
    );
    $target_url = "https://filedrop.gfycat.com";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_URL, $target_url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: multipart/form-data"
    ));
    
    $response = curl_exec($ch);
    
    curl_close($ch);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作