ferrero-opentext/test_upload_standalone.php
DJP 4fda83a2bb Add standalone upload test script for debugging
Standalone PHP script that:
- Gets OAuth token directly
- Creates minimal test image
- Replicates EXACT Postman cURL structure
- Shows verbose output
- Can be run independently: php test_upload_standalone.php

Use to isolate upload issues from main application.
Helps determine if issue is PHP-specific or API restriction.

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

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

246 lines
8 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";
// Load credentials
if (!file_exists('Creds.txt')) {
die("Error: Creds.txt not found\n");
}
$credsContent = file_get_contents('Creds.txt');
preg_match('/Client ID:\s*(.+)/i', $credsContent, $clientIdMatch);
preg_match('/Client Secret:\s*(.+)/i', $credsContent, $clientSecretMatch);
$clientId = trim($clientIdMatch[1] ?? '');
$clientSecret = trim($clientSecretMatch[1] ?? '');
if (empty($clientId) || empty($clientSecret)) {
die("Error: Could not extract credentials from Creds.txt\n");
}
echo "Step 1: Getting OAuth Token...\n";
// Get OAuth token
$tokenUrl = 'https://ppr.dam.ferrero.com/otdsws/oauth2/token';
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $tokenUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query([
'grant_type' => 'client_credentials',
'client_id' => $clientId,
'client_secret' => $clientSecret
]),
CURLOPT_SSL_VERIFYPEER => false
]);
$tokenResponse = curl_exec($ch);
$tokenHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($tokenHttpCode != 200) {
die("Error: Failed to get OAuth token. HTTP Code: $tokenHttpCode\n");
}
$tokenData = json_decode($tokenResponse, true);
$accessToken = $tokenData['access_token'] ?? null;
if (!$accessToken) {
die("Error: No access token in response\n");
}
echo "✅ OAuth Token received\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";