Install dependencies
Install any libraries needed for HTTP requests.
# cURL is built into most PHP installsA 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.
Install any libraries needed for HTTP requests.
# cURL is built into most PHP installsCreate a free account and copy your API key from the dashboard.
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);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";
}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"),
};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";
}
}Apply blur to image backgrounds.
$c = curl_init();
curl_setopt_array($c, [
CURLOPT_URL => "https://fapihub.com/v2/rembg/blur/",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
CURLOPT_POSTFIELDS => [
"image" => new CURLFile("photo.jpg"),
"blur_radius" => "15",
],
]);
file_put_contents("blur_bg.png", curl_exec($c));
curl_close($c);Set RGB color backgrounds.
$c = curl_init();
curl_setopt_array($c, [
CURLOPT_URL => "https://fapihub.com/v2/rembg/color/",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
CURLOPT_POSTFIELDS => [
"image" => new CURLFile("photo.jpg"),
"background_color" => "255,255,255",
],
]);
file_put_contents("white_bg.png", curl_exec($c));
curl_close($c);Render gradient outputs.
$c = curl_init();
curl_setopt_array($c, [
CURLOPT_URL => "https://fapihub.com/v2/rembg/gradient/",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
CURLOPT_POSTFIELDS => [
"image" => new CURLFile("photo.jpg"),
"top_color" => "0,0,200",
"bottom_color" => "255,255,255",
],
]);
file_put_contents("gradient_bg.png", curl_exec($c));
curl_close($c);Add shadow depth controls.
$c = curl_init();
curl_setopt_array($c, [
CURLOPT_URL => "https://fapihub.com/v2/rembg/shadow/",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => ["token: YOUR_API_KEY"],
CURLOPT_POSTFIELDS => [
"image" => new CURLFile("photo.jpg"),
"offset" => "10",
"blur" => "15",
],
]);
file_put_contents("shadow.png", curl_exec($c));
curl_close($c);Free tier includes 100 full-resolution requests/month. No credit card. Your API key is ready in 30 seconds.
Get Your Free API Key