318 lines
10 KiB
PHP
318 lines
10 KiB
PHP
<?php
|
|
// Test script for downloading images with API token authentication
|
|
|
|
// Display errors
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
// Create necessary directories
|
|
$directories = [
|
|
'tmp',
|
|
'images',
|
|
'images/products',
|
|
'log'
|
|
];
|
|
|
|
echo "Setting up test directories...\n";
|
|
foreach ($directories as $dir) {
|
|
if (!file_exists($dir)) {
|
|
if (mkdir($dir, 0777, true)) {
|
|
echo "Created directory: $dir\n";
|
|
} else {
|
|
echo "Failed to create directory: $dir\n";
|
|
}
|
|
} else {
|
|
echo "Directory exists: $dir\n";
|
|
}
|
|
chmod($dir, 0777);
|
|
}
|
|
|
|
// Prompt for API token if not set
|
|
if (!isset($argv[1]) || empty($argv[1])) {
|
|
echo "Please enter your EcomZone API token: ";
|
|
$apiToken = trim(fgets(STDIN));
|
|
} else {
|
|
$apiToken = $argv[1];
|
|
}
|
|
|
|
echo "\nTesting with API token: " . substr($apiToken, 0, 5) . '...' . substr($apiToken, -5) . "\n\n";
|
|
|
|
// Function to make API requests
|
|
function makeApiRequest($endpoint, $apiToken) {
|
|
$apiUrl = 'https://dropship.ecomzone.eu/api/' . ltrim($endpoint, '/');
|
|
|
|
echo "Requesting: $apiUrl\n";
|
|
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $apiUrl,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
CURLOPT_SSL_VERIFYHOST => 0,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Accept: application/json',
|
|
'Authorization: Bearer ' . $apiToken
|
|
]
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
|
$errorMsg = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
echo "HTTP Status Code: $httpCode\n";
|
|
echo "Content Type: $contentType\n";
|
|
|
|
if ($httpCode >= 400) {
|
|
echo "API Error: $errorMsg\n";
|
|
return null;
|
|
}
|
|
|
|
$data = json_decode($response, true);
|
|
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
echo "JSON Decode Error: " . json_last_error_msg() . "\n";
|
|
return null;
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
// Use the specific SKU provided by the user
|
|
$sku = '1817-911';
|
|
echo "Using the specific SKU: $sku\n\n";
|
|
|
|
// Skip catalog fetching step since we have a specific SKU to test
|
|
|
|
// STEP 2: Get detailed product information
|
|
echo "STEP 2: Fetching detailed product information for SKU: $sku\n";
|
|
$productData = makeApiRequest('product/' . urlencode($sku), $apiToken);
|
|
|
|
if (!$productData || empty($productData['data'])) {
|
|
echo "Failed to get product details.\n";
|
|
exit(1);
|
|
}
|
|
|
|
// Extract image URLs from product data
|
|
$imageUrls = [];
|
|
|
|
if (!empty($productData['data']['image'])) {
|
|
$imageUrls[] = $productData['data']['image'];
|
|
echo "Found main image: " . $productData['data']['image'] . "\n";
|
|
}
|
|
|
|
// Try to parse images field which might be JSON string or array
|
|
if (!empty($productData['data']['images'])) {
|
|
if (is_string($productData['data']['images'])) {
|
|
$parsedImages = json_decode($productData['data']['images'], true);
|
|
if (json_last_error() === JSON_ERROR_NONE && is_array($parsedImages)) {
|
|
foreach ($parsedImages as $img) {
|
|
$imageUrls[] = $img;
|
|
echo "Found additional image (from JSON string): $img\n";
|
|
}
|
|
}
|
|
} elseif (is_array($productData['data']['images'])) {
|
|
foreach ($productData['data']['images'] as $img) {
|
|
$imageUrls[] = $img;
|
|
echo "Found additional image (from array): $img\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
if (empty($imageUrls)) {
|
|
echo "No image URLs found in product data.\n";
|
|
exit(1);
|
|
}
|
|
|
|
// STEP 3: Try direct download of the first image with authorization
|
|
echo "\nSTEP 3: Attempting to download the first image directly with Authorization header\n";
|
|
$firstImageUrl = $imageUrls[0];
|
|
echo "Image URL: $firstImageUrl\n";
|
|
|
|
// Try with direct download
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $firstImageUrl,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
CURLOPT_SSL_VERIFYHOST => 0,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Accept: image/webp,image/apng,image/*,*/*;q=0.8',
|
|
'Authorization: Bearer ' . $apiToken
|
|
]
|
|
]);
|
|
|
|
$imageData = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
|
curl_close($ch);
|
|
|
|
echo "HTTP Status Code: $httpCode\n";
|
|
echo "Content Type: $contentType\n";
|
|
|
|
// Check if the response is HTML (authentication error)
|
|
$isHtml = stripos($contentType, 'text/html') !== false ||
|
|
(strlen($imageData) > 15 && stripos($imageData, '<!DOCTYPE html>') !== false);
|
|
|
|
if ($isHtml) {
|
|
echo "FAILED: Authentication error. Received HTML instead of image data.\n";
|
|
file_put_contents('tmp/auth_error.html', $imageData);
|
|
echo "Full HTML response saved to tmp/auth_error.html\n";
|
|
} else {
|
|
$tempFile = 'tmp/direct_download.jpg';
|
|
file_put_contents($tempFile, $imageData);
|
|
$fileSize = filesize($tempFile);
|
|
echo "Downloaded " . number_format($fileSize) . " bytes to $tempFile\n";
|
|
|
|
$imageInfo = @getimagesize($tempFile);
|
|
if ($imageInfo) {
|
|
echo "SUCCESS: Valid image detected\n";
|
|
echo "Image dimensions: " . $imageInfo[0] . 'x' . $imageInfo[1] . ", type: " . $imageInfo['mime'] . "\n";
|
|
} else {
|
|
echo "WARNING: The downloaded file is not a valid image\n";
|
|
}
|
|
}
|
|
|
|
// STEP 4: Check if there's a dedicated API endpoint for downloading images
|
|
echo "\nSTEP 4: Checking if there's a dedicated image download API endpoint\n";
|
|
|
|
// Extract image ID from the URL (last part after the slash)
|
|
$urlParts = explode('/', $firstImageUrl);
|
|
$imageId = end($urlParts);
|
|
|
|
echo "Trying to access image via API using ID: $imageId\n";
|
|
|
|
// Try these possible API endpoints for image download
|
|
$possibleEndpoints = [
|
|
'image/' . $imageId,
|
|
'download/image/' . $imageId,
|
|
'media/' . $imageId,
|
|
'product/image/' . $imageId,
|
|
'product/' . $sku . '/image/' . $imageId
|
|
];
|
|
|
|
$imageDownloaded = false;
|
|
foreach ($possibleEndpoints as $endpoint) {
|
|
echo "\nTrying endpoint: $endpoint\n";
|
|
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => 'https://dropship.ecomzone.eu/api/' . $endpoint,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
CURLOPT_SSL_VERIFYHOST => 0,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Accept: image/webp,image/apng,image/*,*/*;q=0.8',
|
|
'Authorization: Bearer ' . $apiToken
|
|
]
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
|
curl_close($ch);
|
|
|
|
echo "HTTP Status Code: $httpCode\n";
|
|
echo "Content Type: $contentType\n";
|
|
|
|
if ($httpCode === 200 && !stripos($contentType, 'text/html')) {
|
|
$tempFile = 'tmp/api_' . basename($endpoint) . '.jpg';
|
|
file_put_contents($tempFile, $response);
|
|
|
|
$imageInfo = @getimagesize($tempFile);
|
|
if ($imageInfo) {
|
|
echo "SUCCESS: Valid image downloaded via API endpoint\n";
|
|
echo "Image dimensions: " . $imageInfo[0] . 'x' . $imageInfo[1] . ", type: " . $imageInfo['mime'] . "\n";
|
|
$imageDownloaded = true;
|
|
} else {
|
|
echo "Response doesn't appear to be a valid image\n";
|
|
unlink($tempFile);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!$imageDownloaded) {
|
|
echo "\nNone of the attempted API endpoints worked for direct image download.\n";
|
|
}
|
|
|
|
// STEP 5: Try using an alternative base URL for the image
|
|
echo "\nSTEP 5: Trying alternative base URLs for the image\n";
|
|
|
|
// Extract the path part from the URL
|
|
$parsedUrl = parse_url($firstImageUrl);
|
|
$imagePath = $parsedUrl['path'] ?? '';
|
|
|
|
if (empty($imagePath)) {
|
|
echo "Could not extract image path from URL.\n";
|
|
} else {
|
|
echo "Image path: $imagePath\n";
|
|
|
|
// Try with alternative base URLs
|
|
$alternativeBaseUrls = [
|
|
'https://api.dropship.ecomzone.eu',
|
|
'https://cdn.dropship.ecomzone.eu',
|
|
'https://media.dropship.ecomzone.eu',
|
|
'https://images.dropship.ecomzone.eu'
|
|
];
|
|
|
|
foreach ($alternativeBaseUrls as $baseUrl) {
|
|
$fullUrl = $baseUrl . $imagePath;
|
|
echo "\nTrying alternative URL: $fullUrl\n";
|
|
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $fullUrl,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_TIMEOUT => 30,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
CURLOPT_SSL_VERIFYHOST => 0,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Accept: image/webp,image/apng,image/*,*/*;q=0.8',
|
|
'Authorization: Bearer ' . $apiToken
|
|
]
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
|
curl_close($ch);
|
|
|
|
echo "HTTP Status Code: $httpCode\n";
|
|
echo "Content Type: $contentType\n";
|
|
|
|
if ($httpCode === 200 && !stripos($contentType, 'text/html')) {
|
|
$tempFile = 'tmp/alt_domain_' . str_replace(['https://', '.'], ['', '_'], $baseUrl) . '.jpg';
|
|
file_put_contents($tempFile, $response);
|
|
|
|
$imageInfo = @getimagesize($tempFile);
|
|
if ($imageInfo) {
|
|
echo "SUCCESS: Valid image downloaded from alternative URL\n";
|
|
echo "Image dimensions: " . $imageInfo[0] . 'x' . $imageInfo[1] . ", type: " . $imageInfo['mime'] . "\n";
|
|
} else {
|
|
echo "Response doesn't appear to be a valid image\n";
|
|
unlink($tempFile);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Summary
|
|
echo "\n=== SUMMARY ===\n";
|
|
echo "Product SKU: $sku\n";
|
|
echo "Found " . count($imageUrls) . " image URLs in product data\n";
|
|
echo "Direct image downloads with Authorization header don't work - server returns HTML login page\n";
|
|
|
|
echo "\nPOSSIBLE SOLUTIONS:\n";
|
|
echo "1. The EcomZone API doesn't support direct image downloads through Authorization headers\n";
|
|
echo "2. We may need to use a different authentication method for images\n";
|
|
echo "3. Images might require a signed URL or temporary access token\n";
|
|
echo "4. Consider contacting EcomZone support for the correct way to download images\n";
|
|
echo "\nTest completed\n";
|