0

我想使用https://remove.bg api 从他们的文档中删除图像背景,因为我不熟悉 curl 这里是我想出的

$url = "https://api.remove.bg/v1.0/removebg";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'x-api-key: my-api-key',
    'image_url:https://example.com/image-to-remove-bg.png'
));

$server_output = curl_exec ($ch);
curl_close ($ch);

print_r($server_output);

但它返回的是空的身体;请你帮帮我或指出我哪里做错了。

4

1 回答 1

1

image_url应该作为 POST 字段传递,而不是作为标题传递。所以这是你的修改代码:

$url = "https://api.remove.bg/v1.0/removebg";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'x-api-key:my-api-key',
]);

// move image_url here:
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'image_url' => 'https://example.com/image-to-remove-bg.png',
]);

$server_output = curl_exec($ch);
curl_close($ch);

print_r($server_output);
于 2019-11-23T15:20:07.687 回答