nadermx / backgroundremover

Background Remover lets you Remove Background from images and video using AI with a simple command line interface that is free and open source.
https://www.backgroundremoverai.com
MIT License
6.46k stars 538 forks source link

API Curl request with PHP not working #60

Closed azfarahmed closed 1 year ago

azfarahmed commented 1 year ago

I paid for credits but API is not working with php, i tried every possible way. It always says -

{"error": "Missing files"}{"error": "Missing files"}stdClass Object ( [error] => Missing files ) Missing files

Even though I have placed correct image path.

Here is the code

`<?php ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

$headers = array("Authorization: c02219f9dee7427b92d1847a61f1db17"); $file_list = ['/home/xofile/public_html/app.xofile.com/public/testfiles/blah.jpeg']; $api_url = 'https://api.backgroundremover.app/v1/convert/'; $results_url = 'https://api.backgroundremover.app/v1/results/'; $post_data = [];

function download_file($url, $filename){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_SSLVERSION, 3); $data = curl_exec($curl); $error = curl_error($curl); curl_close ($curl); $file = fopen("/home/xofile/public_html/app.xofile.com/public/$filename", "w+"); fputs($file, $data); fclose($file); }

function convert_files($file_list, $headers, $api_url) { $post_data['lang'] = 'en'; $post_data['convert_to'] = 'image-backgroundremover';

foreach ($file_list as $index => $file) {
    $post_data['file[' . $index . ']'] = curl_file_create(
        realpath($file),
        mime_content_type($file),
        basename($file)
    );
    echo "<img src='".realpath($file)."'/>";
}

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($curl);
curl_close($curl);
print($content);

return $content;

}

function get_results($params, $api_url, $headers) { $error = json_decode($params); print_r($params); print_r($error);

if ($error->error) {
    print_r($error->error);
    return;
}

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$content = json_decode(curl_exec($curl));
curl_close($curl);

while (!$content->finished) {
    if (intval($content->queue_count) > 0) {
        print_r("queue: $content->queue_count");
    }

    sleep(5);
    $results = get_results($params, $api_url, $headers);
}

foreach ($content->files as $f) {
    download_file($f->url, $f->filename);
}

}

get_results(convert_files($file_list, $headers, $api_url), $api_url, $headers); ?> `

Could please explain with simple example.

nadermx commented 1 year ago

This was just fixed, please check https://backgroundremoverai.com/api/#settings-i18n-introduction