简体   繁体   English

PHP curl:如何在命令行中传递具有数组语法的参数?

[英]PHP curl: How to pass a parameter that has array syntax in the command line?

Sorry for the vague title, I don't use curl often enough to know what the proper terms are.抱歉标题含糊不清,我不经常使用 curl 足以知道正确的术语是什么。 Basically I'm trying to convert a commandline curl call that involves a file upload into a PHP script using curl_init etc.基本上我正在尝试使用 curl_init 等将涉及文件上传的命令行 curl 调用转换为 PHP 脚本。

The commandline curl has parameters like -F query='...' -F variables[file]=@/mnt/d/temp/test.jpg .命令行 curl 具有-F query='...' -F variables[file]=@/mnt/d/temp/test.jpg test.jpg 等参数。 Calling the curl command via commandline works fine, so the backend is working.通过命令行调用 curl 命令可以正常工作,因此后端可以正常工作。 My issue is when I try it using PHP.我的问题是当我使用 PHP 尝试它时。 My issue is I don't know how to properly convert that -F variables[file]=... syntax into the PHP curl side.我的问题是我不知道如何正确地将-F variables[file]=...语法转换为 PHP curl 端。

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.不幸的是,它返回 HTTP 500。

I also try to replace the $cFile variable with $cFile = '@'. realpath("/mnt/d/temp/test.jpg");我还尝试将 $cFile 变量替换为$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 ) (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] :事实证明,我通过假设-F variables[file]=...语法是某种数组而想多了,而我真正需要的只是提交一个名为variable[file]的 post 变量:

$cFile = curl_file_create("/mnt/d/temp/test.jpg");
$post = array('query' => $query,'variables[file]' => $cFile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM