我正在尝试将图像从我的服务器上传到安装chevereto的img服务器,但我无法让它工作。
到目前为止,我的代码如下:
$directory = "/var/www";
$images = glob($directory . "/*.jpg");
foreach($images as $image)
{
echo $image;
$data = base64_encode(file_get_contents($image));
$run= shell_exec("curl --location --request POST \"http://ip/api/1/upload/?key=123456789&source=$data&format=json\"");
print_r($run);
}获取错误:414 Request-URI Too Large
下面是api文档:https://chevereto.com/docs/api-v1
发布于 2020-05-30 05:22:52
您使用的是POST请求方法,但您正在发送source parameter in URL。您的URL中的$data是base64编码的,这将导致一个非常大的字符串。这就是为什么您要获得错误的414 Request-URI Too Large。
解决方案:
您应该使用相同的POST方法将params更改为JSON主体。
从您提供的chevereto文档中可以清楚地看到这一点。
API v1调用可以使用POST或GET请求方法进行,但是由于GET请求受允许长度的限制,所以您应该更喜欢POST请求方法。
因此,使用POST方法将包含参数的JSON作为key:value对。这将解决这个问题。
示例PHP代码:
$requestData = [
'key' => 123456789,
'source' => 'base64EncodedStringHere',
'format' => 'json'
];
$jsonData = json_encode($jsonData);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip/api/1/upload",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $jsonData,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;https://stackoverflow.com/questions/62097636
复制相似问题