0

I wanted to remove image background using the https://remove.bg api from their documentation since am new to use of curl here is what i have come up with

$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);

But it's returning empty body; will you please help me out or point me where am doing it wrong.

Mig82
  • 4,856
  • 4
  • 40
  • 63
Stack
  • 11
  • 6

1 Answers1

1

image_url should be passed as POST field, not as header. So here is your code with modification:

$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);
Konstantin Bogomolov
  • 1,278
  • 1
  • 11
  • 16