Sorry for the vague title, I don't use curl often enough to know what the proper terms are. Basically I'm trying to convert a commandline curl call that involves a file upload into a PHP script using curl_init etc.
The commandline curl has parameters like -F query='...' -F variables[file]=@/mnt/d/temp/test.jpg
. Calling the curl command via commandline works fine, so the backend is working. My issue is when I try it using PHP. My issue is I don't know how to properly convert that -F variables[file]=...
syntax into the PHP curl side.
Given that, I try this:
$query = '...';
$cFile = curl_file_create("/mnt/d/temp/test.jpg");
$variables = array("file" => $cFile);
$post = array('query' => $query,'variables' => $variables);
curl_setopt($chObj, CURLOPT_POSTFIELDS, $post);
Unfortunately, it returns HTTP 500.
I also try to replace the $cFile variable with $cFile = '@'. realpath("/mnt/d/temp/test.jpg");
$cFile = '@'. realpath("/mnt/d/temp/test.jpg");
, same result.
(This is also my first time uploading a file using PHP curl, my reference for the above code was this SO question: how to upload file using curl with php )
Any advice would be appreciated. (I would just Google it myself, but IDK the proper terms to Google.)
Ok, obviously after posting on SO I figure it out after a bit more fiddling around, so posting my own answer.
It turns out I was overthinking it by assuming the -F variables[file]=...
syntax was some kind of array, when really all I needed was to submit a post variable named variable[file]
:
$cFile = curl_file_create("/mnt/d/temp/test.jpg");
$post = array('query' => $query,'variables[file]' => $cFile);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.