【问题标题】:Returning response from remove.bg api从 remove.bg api 返回响应
【发布时间】:2019-11-23 14:54:40
【问题描述】:

我想从他们的文档中使用https://remove.bg api 删除图像背景,因为我是新来使用 curl 的,这是我想出的

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

但它返回的是空的身体;请您帮助我或指出我哪里做错了。

【问题讨论】:

    标签: php api curl imagebackground


    【解决方案1】:

    image_url 应该作为 POST 字段传递,而不是作为标题。所以这是你的修改代码:

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

    【讨论】:

    • 感谢它的工作,该 api 在 localhost 上不起作用,这是另一个问题。
    猜你喜欢
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-05
    • 2019-03-16
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多