Reputation: 45
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.
Upvotes: 0
Views: 1435
Reputation: 45
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);
Upvotes: 2
Reputation: 21513
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.
Upvotes: 1