Error With Manual Signature Creation via PHP

LivinLL
LivinLL Member Posts: 1

I am using the manual method, not SDK, to generate a signature. This is a test page that generates a curl that I run via my windows command prompt.

<?php
// Enable error reporting to display errors directly on the screen
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// API credentials
$cloud_name = '[removed for security but in live version it's there';
$api_key = '[same - in live it's there]';
$api_secret = '[same]';

// Parameters to sign (include folder)
$params_to_sign = [
'folder' => 'test_uploads', // Folder for uploaded images
'timestamp' => time(), // Current timestamp
];

// Sort parameters alphabetically by key
ksort($params_to_sign);

// Generate the string to sign
$signature_string = http_build_query($params_to_sign, '', '&');
$signature = hash_hmac('sha256', $signature_string, $api_secret);

// Generate cURL command
$file_url = 'https://upload.wikimedia.org/wikipedia/commons/b/b1/VAN_CAT.png'; // Test file URL
$curl_command = "curl -d \"file=$file_url&api_key=$api_key&folder=test_uploads&timestamp={$params_to_sign['timestamp']}&signature=$signature\" -X POST https://api.cloudinary.com/v1_1/$cloud_name/image/upload";

// Output debugging information and cURL command
header('Content-Type: text/plain');
echo "Debugging Information:\n\n";
echo "Generated Signature String:\n$signature_string\n\n";
echo "Generated Signature:\n$signature\n\n";
echo "Generated Timestamp:\n{$params_to_sign['timestamp']}\n\n";
echo "Generated cURL Command:\n$curl_command\n";
?>

= = = = =

This is a curl generated by it

curl -d "file=https://upload.wikimedia.org/wikipedia/commons/b/b1/VAN_CAT.png&api_key=[removed]&folder=test_uploads&timestamp=1733421643&signature=[long string removed]" -X POST https://api.cloudinary.com/v1_1/dd19jhkar/image/upload

This is the error message I get.
{"error":{"message":"Invalid Signature 5b037741284cfac33c9f02ee7ea0fd4712ff7636d727d4aedcec67b8163a72a6. String to sign - 'folder=test_uploads&timestamp=1733421643'."}}
= = = =

Here is a screenshot of my default signed, upload preset on Cloudinary that I DO have set properly as the default for API etc.

https://www.screencast.com/t/pY0QjZAYGCg

Any help is appreciated.