Cloudinary REST API destroy接口调用无返回无法删除图片,该如何排查?
Cloudinary 删除接口调用问题修复方案
问题排查
- 签名计算规则错误:Cloudinary 要求签名生成必须包含所有非
api_key/api_secret/file的请求参数,且按键名字典序升序拼接后再追加api_secret做哈希。你当前仅拼接了timestamp参数,缺失public_id、invalidate参数,会直接触发签名验证失败。 - 错误返回被拦截:你开启了
CURLOPT_FAILONERROR = true配置,当接口返回4xx/5xx状态码时,curl_exec会直接返回布尔值false而非错误响应内容,导致你看不到任何返回结果,需要补充curl错误打印逻辑。 - 配置占位符未替换:请求URL中的
CLOUDNAME需要替换为你自己Cloudinary账号的云名称,未替换的情况下请求会直接404。 - 冗余参数干扰:
resource_type已经作为路径参数放在URL的/image/位置,不需要再放到POST请求体中,冗余参数会导致签名计算不匹配。
修复后代码示例
$api_key = "你的实际api_key"; $api_secret = "你的实际api_secret"; $cloud_name = "你的实际云名称"; $timestamp = time(); $public_id = "folder/sample_public_id"; $invalidate = true; // 按规则生成签名:所有参与签名的参数按键升序排列拼接 $sign_params = [ 'invalidate' => $invalidate, 'public_id' => $public_id, 'timestamp' => $timestamp ]; ksort($sign_params); $sign_str = ''; foreach ($sign_params as $k => $v) { $sign_str .= $k . '=' . $v . '&'; } $sign_str = rtrim($sign_str, '&') . $api_secret; $signature = sha1($sign_str); $postRequest = [ 'public_id' => $public_id, 'timestamp' => $timestamp, 'api_key' => $api_key, 'signature' => $signature, 'invalidate' => $invalidate ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"https://api.cloudinary.com/v1_1/{$cloud_name}/image/destroy"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postRequest)); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,3); curl_setopt($ch,CURLOPT_TIMEOUT, 20); $response = curl_exec($ch); // 补充错误打印 if(curl_errno($ch)) { echo 'Curl错误:' . curl_error($ch); } else { print_r($response); } curl_close($ch);
内容的提问来源于stack exchange,提问作者morko
相关产品推荐
相关产品推荐

