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

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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用gfycat API创建一个gfycat,通过curl使用php上传文件,但它没有 工作和var_dump($ response)给了我bool(假)。

我的代码:

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

非常感谢帮助。 谢谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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);
    
    已采纳该答案
    打赏 评论
  • dongzi8191 2018-06-11 10:04

    when debugging curl code, it's often a good idea to enable CURLOPT_VERBOSE and check the stderr log. further more, if curl_exec reutrned bool(false), it means there was a problem with the transfer, and you can use the curl_error() function to get an error message. and lastly, don't set the header "Content-Type: multipart/form-data" manually, curl will set that header for you, and unlike you, curl won't make any typos in doing so, and worse, you risk overwriting/removing the boundary parameter of the header.

    try

    $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 );
    try {
        $stderrh = tmpfile ();
        curl_setopt_array ( $ch, array (
                CURLOPT_VERBOSE => 1,
                CURLOPT_STDERR => $stderrh 
        ) );
        $response = curl_exec ( $ch );
        if ($response === false) {
            throw new \RuntimeException ( "curl error " . curl_errno ( $ch ) . ": " . curl_error ( $ch ) . " - verbose log: " . file_get_contents ( stream_get_meta_data ( $stderrh ) ['uri'] ) ); // https://bugs.php.net/bug.php?id=76268
        }
    } finally{
        curl_setopt_array ( $ch, array (
                CURLOPT_VERBOSE => 0,
                CURLOPT_STDERR => STDERR 
        ) );
        fclose ( $stderrh );
    }
    var_dump ( $response ); // bool(false) here
    curl_close ( $ch );
    

    now, if there is an error, it should give you a nice detailed log of what happened up to to the curl error, in the exception error log.

    打赏 评论

相关推荐 更多相似问题