Search code examples
phpcurlpostcloudinary

Cloudinary REST api destroy doesn't work?


I'm trying to delete an image in Cloudinary but it doesn't work, where am I doing wrong?

this code returns nothing result

$api_key = "----";
$api_secret = "---";
$timestamp = time();
$signature = sha1("timestamp=".$timestamp.$api_secret);

$postRequest = array(
'public_id' => "folder/sample_public_id",
'timestamp' => $timestamp,
'api_key' => $api_key,
'signature' => $signature,
'resource_type' => 'image',
'invalidate' => true);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.cloudinary.com/v1_1/CLOUDNAME/image/destroy");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postRequest));
curl_setopt($ch, CURLOPT_FAILONERROR, true); 
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);
print_r( curl_exec($ch) );

Solution

  • Without having additional details about the issue and just by looking at the code, I see a reason which would explain why such a request is not succeeding.

    When you are generating authentication signatures for API calls to Cloudinary, you need to sign all parameters you're passing as part of your request apart from file, cloud_name, resource_type and your api_key.

    In your case, you are only signing the timestamp but in your request, you are also passing the public_id and invalidate parameters. Therefore, you need to include them in your signature generation line.

    Note that the parameters need to be ordered alphabetically when signing and the api_secret concatenated at the end. In this case, your 4th line to generate the signature should look like this:

    $signature = sha1("invalidate=true&public_id=folder/sample_public_id&timestamp=".$timestamp.$api_secret);
    

    As a first step, I'd recommend making the above changes and sending another request. If that doesn't succeed then please update the post with additional information.