ferrero-opentext/test_upload_standalone.php
DJP cbdf3f60a7 Simplify standalone test - accept token as argument + show token in UI
Standalone test now accepts token as command line argument instead of
trying to get new token (credentials may have changed).

UI now shows OAuth token in expandable section for copying to standalone test.

Usage: php test_upload_standalone.php "eyJraWQiOiI0Y..."

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-28 08:39:25 -04:00

211 lines
7.2 KiB
PHP

<?php
/**
* Standalone Upload Test - Replicates exact Postman cURL
* Run from command line: php test_upload_standalone.php
*/
echo "=== Ferrero Upload Test - Standalone ===\n\n";
// SIMPLE VERSION: Accept token as command line argument
// Usage: php test_upload_standalone.php "your_bearer_token"
if ($argc < 2) {
echo "Usage: php test_upload_standalone.php \"Bearer_token_from_workflow\"\n\n";
echo "To get the token:\n";
echo "1. Go to workflow_v3.php in browser\n";
echo "2. Open browser console\n";
echo "3. Look at network requests for Authorization header\n";
echo "OR: Just run upload from workflow and check MAMP error log for token\n\n";
die("Error: No OAuth token provided\n");
}
$accessToken = $argv[1];
echo "Using provided OAuth token\n";
echo "Token starts with: " . substr($accessToken, 0, 20) . "...\n\n";
// Check if test file exists
$testFile = __DIR__ . '/test_upload.jpg';
if (!file_exists($testFile)) {
echo "Creating test file: test_upload.jpg\n";
// Create a 1x1 pixel red JPEG
$img = imagecreatetruecolor(100, 100);
$red = imagecolorallocate($img, 255, 0, 0);
imagefill($img, 0, 0, $red);
imagejpeg($img, $testFile);
imagedestroy($img);
}
echo "Test file: $testFile\n";
echo "File size: " . filesize($testFile) . " bytes\n\n";
echo "Step 2: Uploading file...\n";
// EXACT structure from successful Postman cURL
$assetRepresentation = json_encode([
'asset_resource' => [
'asset' => [
'metadata' => [
'metadata_element_list' => [
[
'id' => 'FERRERO.FIELD.MKTG.ASSET TYPE',
'type' => 'com.artesia.metadata.MetadataField',
'value' => [
'cascading_domain_value' => false,
'domain_value' => true,
'value' => [
'type' => 'string',
'value' => 'heroimage'
]
]
],
[
'id' => 'FERRERO.FIELD.FISCAL YEAR',
'type' => 'com.artesia.metadata.MetadataField',
'value' => [
'cascading_domain_value' => false,
'domain_value' => true,
'value' => [
'type' => 'string',
'value' => '2025/2026'
]
]
],
[
'id' => 'MAIN_LANGUAGES',
'parent_table_id' => 'FERRERO.TABULAR.FIELD.MAIN LANGUAGES',
'type' => 'com.artesia.metadata.MetadataTableField',
'values' => [
[
'cascading_domain_value' => false,
'domain_value' => true,
'value' => [
'field_value' => [
'type' => 'string',
'value' => 'IT'
],
'type' => 'com.artesia.metadata.DomainValue'
]
]
]
],
[
'id' => 'ARTESIA.FIELD.ASSET NAME',
'type' => 'com.artesia.metadata.MetadataField',
'value' => [
'cascading_domain_value' => false,
'domain_value' => true,
'value' => [
'type' => 'string',
'value' => 'test_upload.jpg'
]
]
],
[
'id' => 'FERRERO.FIELD.STATE',
'type' => 'com.artesia.metadata.MetadataField',
'value' => [
'cascading_domain_value' => false,
'domain_value' => true,
'value' => [
'type' => 'string',
'value' => 'Local'
]
]
]
]
],
'metadata_model_id' => 'ECOMMERCE',
'security_policy_list' => [
['id' => 1594]
]
]
]
]);
$manifest = json_encode([
'upload_manifest' => [
'master_files' => [
[
'file' => [
'file_name' => 'test_upload.jpg',
'file_type' => 'image/jpeg'
]
]
]
]
]);
$folderId = 'e96080ba0cd1427d253a28a87504b6665eaa02cb';
// Prepare multipart data
$postFields = [
'asset_representation' => $assetRepresentation,
'parent_folder_id' => $folderId,
'manifest' => $manifest,
'files' => new CURLFile($testFile, 'image/jpeg', 'test_upload.jpg')
];
echo "Upload URL: https://ppr.dam.ferrero.com/otmmapi/v6/assets\n";
echo "Folder ID: $folderId\n";
echo "Asset Representation size: " . strlen($assetRepresentation) . " bytes\n";
echo "Manifest size: " . strlen($manifest) . " bytes\n\n";
// Upload
$ch = curl_init();
$cookieFile = sys_get_temp_dir() . '/ferrero_test_cookies.txt';
curl_setopt_array($ch, [
CURLOPT_URL => 'https://ppr.dam.ferrero.com/otmmapi/v6/assets',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postFields,
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'Authorization: Bearer ' . $accessToken
],
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 60,
CURLOPT_COOKIEJAR => $cookieFile,
CURLOPT_COOKIEFILE => $cookieFile,
CURLOPT_HEADER => true,
CURLOPT_VERBOSE => true
]);
echo "Executing upload...\n";
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$curlError = curl_error($ch);
curl_close($ch);
echo "\n=== RESPONSE ===\n";
echo "HTTP Code: $httpCode\n";
if ($curlError) {
echo "cURL Error: $curlError\n";
}
$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
echo "\nResponse Headers:\n";
echo $headers . "\n";
echo "\nResponse Body:\n";
echo $body . "\n";
if ($httpCode == 201 || $httpCode == 202) {
echo "\n✅ SUCCESS! Upload accepted!\n";
$responseData = json_decode($body, true);
if (isset($responseData['asset_resource_list'])) {
echo "Asset ID: " . ($responseData['asset_resource_list']['asset_resource'][0]['asset']['asset_id'] ?? 'N/A') . "\n";
}
} else {
echo "\n❌ FAILED with HTTP $httpCode\n";
$responseData = json_decode($body, true);
if (isset($responseData['exception_body'])) {
echo "Error: " . $responseData['exception_body']['message'] . "\n";
echo "Debug: " . $responseData['exception_body']['debug_message'] . "\n";
}
}
echo "\nDone.\n";