[英]PHP curl: How to pass a parameter that has array syntax in the command line?
抱歉標題含糊不清,我不經常使用 curl 足以知道正確的術語是什么。 基本上我正在嘗試使用 curl_init 等將涉及文件上傳的命令行 curl 調用轉換為 PHP 腳本。
命令行 curl 具有-F query='...' -F variables[file]=@/mnt/d/temp/test.jpg
test.jpg 等參數。 通過命令行調用 curl 命令可以正常工作,因此后端可以正常工作。 我的問題是當我使用 PHP 嘗試它時。 我的問題是我不知道如何正確地將-F variables[file]=...
語法轉換為 PHP curl 端。
鑒於此,我試試這個:
$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);
不幸的是,它返回 HTTP 500。
我還嘗試將 $cFile 變量替換為$cFile = '@'. realpath("/mnt/d/temp/test.jpg");
$cFile = '@'. realpath("/mnt/d/temp/test.jpg");
,同樣的結果。
(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 )
任何意見,將不勝感激。 (我自己會用谷歌搜索,但要知道谷歌的正確條款。)
好的,很明顯,在我發帖后,我在多擺弄后弄明白了,所以發布我自己的答案。
事實證明,我通過假設-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.