How to Remove Image Backgrounds in PHP

A complete guide to integrating FAPIhub's background removal API into your PHP project. Takes less than 10 minutes.

This guide shows how to call the FAPIhub background removal API from PHP using the built-in cURL extension. No Composer packages required — cURL is available in most PHP 8.1+ installations by default.

Prerequisites

  • A FAPIhub API key (get one free at fapihub.com/dashboard)
  • PHP 8.1 or higher installed
  • Basic understanding of HTTP requests

Step-by-Step Integration

1

Install dependencies

Install any libraries needed for HTTP requests.

# cURL is built into most PHP installs
2

Get your API key

Create a free account and copy your API key from the dashboard.

3

Basic background removal

Upload an image and receive transparent PNG output.

$c = curl_init();
curl_setopt_array($c, [
  CURLOPT_URL => "https://fapihub.com/v2/rembg/",
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_POST => 1,
  CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
  CURLOPT_POSTFIELDS => ["image" => new CURLFile("photo.jpg")]
]);
$res = curl_exec($c);
4

Save and verify output

Write API response bytes to a PNG file and verify output.

$result = curl_exec($c);
$http_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);

if ($http_code === 200) {
    file_put_contents("result.png", $result);
    echo "Saved result.png\n";
} else {
    echo "Error: HTTP $http_code\n";
}
5

Handle errors

Check HTTP status codes and catch network errors.

$c = curl_init();
curl_setopt_array($c, [
    CURLOPT_URL => "https://fapihub.com/v2/rembg/",
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_POST => 1,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
    CURLOPT_POSTFIELDS => ["image" => new CURLFile("photo.jpg")],
]);

$result = curl_exec($c);
$error = curl_error($c);
$http_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);

if ($error) {
    throw new RuntimeException("cURL error: $error");
}

match ($http_code) {
    200 => file_put_contents("result.png", $result),
    401 => throw new RuntimeException("Invalid API key"),
    429 => throw new RuntimeException("Rate limit reached — retry after delay"),
    default => throw new RuntimeException("API error: HTTP $http_code"),
};
6

Batch processing

Loop through image paths for high-volume jobs.

$files = glob("images/*.jpg");
$output_dir = "results/";

if (!is_dir($output_dir)) mkdir($output_dir);

foreach ($files as $file) {
    $c = curl_init();
    curl_setopt_array($c, [
        CURLOPT_URL => "https://fapihub.com/v2/rembg/",
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_POST => 1,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
        CURLOPT_POSTFIELDS => ["image" => new CURLFile($file)],
    ]);

    $result = curl_exec($c);
    $http_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
    curl_close($c);

    if ($http_code === 200) {
        $out = $output_dir . pathinfo($file, PATHINFO_FILENAME) . ".png";
        file_put_contents($out, $result);
        echo "Done: $file\n";
    } else {
        echo "Error on $file: HTTP $http_code\n";
    }
}

Original vs Result

See what the API returns on real examples. Each pair shows the untouched source image and the cleaned FAPIhub result.

Ready to Start?

Free tier includes 100 full-resolution requests/month. No credit card. Your API key is ready in 30 seconds.

Get Your Free API Key