0

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");, 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.)

3

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.