gpt4 book ai didi

php - 图片上传 CURL 命令到 PHP Curl

转载 作者:可可西里 更新时间:2023-11-01 12:36:09 25 4
gpt4 key购买 nike

我是 PHP CURL 的新手,正在尝试调用 API 来上传图片(一次上传多个文件)。 API 文档在 CURL 中给出了以下示例。我已经测试过,它正在运行。

curl -H "Authorization: Bearer 123456789" -i -X POST -F "whitespace=1" \
-F "imageData[]=@/path/to/images/milk.jpg" \
-F "imageData[]=@/path/to/images/peas.jpg" \
https://mytestapi.com/1.0/uploadimages

现在我需要将它转换为 PHP Curl。出于某种原因,我总是收到错误“imageData”参数无效。有人可以帮忙吗

$token = '123456789';
$imgUrl = 'https://mytestapi.com/1.0/uploadimages';


$data_to_post = array();
$data_to_post['whitespace'] = '1';
$data_to_post['imageData[]'] = '@/path/to/images/milk.jpg';
$data_to_post['imageData[]'] = '@/path/to/images/peas.jpg';


$curl = curl_init($imgUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$token]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post);
$data = json_decode(curl_exec($curl));
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

var_dump($data);

最佳答案

尝试给出图像的完整路径而不是'@/path/to/images/milk.jpg';

这是更新后的代码:

$token = '123456789';
$imgUrl = 'https://mytestapi.com/1.0/uploadimages';


$data_to_post = array();
$data_to_post['whitespace'] = '1';
$data_to_post['imageData'][] = "@".$imgUrl.'/path/to/images/milk.jpg';
$data_to_post['imageData'][] = "@".$imgUrl.'/path/to/images/peas.jpg';


$curl = curl_init($imgUrl);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$token]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl,CURLOPT_POST, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $data_to_post);
$data = json_decode(curl_exec($curl));
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

var_dump($data);

如果这不起作用,请告诉我。

关于php - 图片上传 CURL 命令到 PHP Curl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34459925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com