Search code examples
phpapicurlremove.bg

Returning response from remove.bg api


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.


Solution

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