卷曲的新手,我想使用以下结构发送带有标题的卷曲请求
$ curl -H 'X-API-Key: INSERT_YOUR_API_KEY_HERE' \
-F 'image_url=https://www.remove.bg/example.jpg' \
-F 'size=auto' \
-f https://api.remove.bg/v1.0/removebg -o no-bg.png
这是代码正在使用
function cUrl($url, $post_fields = null, $headers = null) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
if ($post_fields && !empty($post_fields)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
if ($headers && !empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $data;
}
$url = "https://api.remove.bg/v1.0/removebg";
$post_fields = 'image_url=https://example.com/1.png&size=auto';
$headers = array(
'Content-Type' => 'application/x-www-form-urlencoded',
'charset' => 'utf-8',
'X-API-Key'=>'1234567890'
);
$data = $bg->cUrl($url, $post_fields, $headers);
这就是正在得到的
{“错误”:[{“标题”:“缺少API 密钥”,“代码”:“ auth_failed”,“详细信息”:“请通过提供您的API密钥 X-Api-Key请求标头“}]}
我如何发送此请求?