workingCopy/ecomzone/api_test.php
2025-06-24 21:46:54 +02:00

143 lines
4.7 KiB
PHP

<?php
// Simple API test script that doesn't rely on PrestaShop configuration
// Display errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Define required constants
if (!defined('_PS_VERSION_')) {
define('_PS_VERSION_', '8.0.0');
}
if (!defined('_PS_MODULE_DIR_')) {
define('_PS_MODULE_DIR_', __DIR__ . '/../../modules/');
}
if (!defined('_PS_ROOT_DIR_')) {
define('_PS_ROOT_DIR_', dirname(__DIR__, 2));
}
// Include required files directly
require_once(__DIR__ . '/classes/EcomZoneException.php');
require_once(__DIR__ . '/classes/EcomZoneLogger.php');
require_once(__DIR__ . '/classes/EcomZoneClient.php');
// Set headers for plain text output
header('Content-Type: text/plain');
header('Cache-Control: no-cache, must-revalidate');
echo "EcomZone API Test (Standalone)\n";
echo "============================\n\n";
// Create a mock Configuration class if it doesn't exist
if (!class_exists('Configuration')) {
class Configuration {
public static function get($key, $default = null) {
$config = [
'ECOMZONE_API_URL' => 'https://dropship.ecomzone.eu/api',
'ECOMZONE_API_TOKEN' => 'klRyAdrXaxL0s6PEUp7LDlH6T8aPSCtBY8NiEHsHiWpc6646K2TZPi5KMxUg' // Replace with your actual API token
];
return isset($config[$key]) ? $config[$key] : $default;
}
public static function updateValue($key, $value) {
return true;
}
}
}
// Mock PrestaShop classes
if (!class_exists('Context')) {
class Context {
public $shop;
public static $instance;
public function __construct() {
$this->shop = new stdClass();
$this->shop->id = 1;
}
public static function getContext() {
if (!self::$instance) {
self::$instance = new Context();
}
return self::$instance;
}
}
}
if (!class_exists('PrestaShopLogger')) {
class PrestaShopLogger {
public static function addLog($message, $severity = 1, $error_code = null, $object_type = null, $object_id = null, $allow_duplicate = false, $id_employee = null) {
return true;
}
}
}
// Create log directory if it doesn't exist
$logDir = __DIR__ . '/log';
if (!file_exists($logDir)) {
mkdir($logDir, 0755, true);
}
try {
// Get API settings
$apiUrl = Configuration::get('ECOMZONE_API_URL');
$apiToken = Configuration::get('ECOMZONE_API_TOKEN');
echo "API URL: " . $apiUrl . "\n";
echo "API Token: " . (empty($apiToken) || $apiToken === 'YOUR_API_TOKEN' ?
"Not set - Please edit api_test.php and replace YOUR_API_TOKEN with your actual token" :
"Set (length: " . strlen($apiToken) . " chars)") . "\n\n";
if (empty($apiToken) || $apiToken === 'YOUR_API_TOKEN') {
throw new Exception("API token is not configured. Please edit api_test.php and replace YOUR_API_TOKEN with your actual token.");
}
echo "Testing API connection...\n\n";
// Create API client
$client = new EcomZoneClient();
// Test API connectivity with catalog endpoint
echo "Requesting catalog data (page 1, limit 1)...\n";
$result = $client->getCatalog(1, 1);
if (isset($result['data']) && is_array($result['data'])) {
echo "Success! Received " . count($result['data']) . " product(s)\n";
echo "Total products available: " . ($result['total'] ?? 'unknown') . "\n\n";
if (!empty($result['data'])) {
$sku = $result['data'][0]['sku'] ?? null;
if ($sku) {
echo "Testing product detail retrieval...\n";
echo "Requesting product with SKU: " . $sku . "\n";
$productDetail = $client->getProduct($sku);
if (isset($productDetail['data']) && !empty($productDetail['data'])) {
echo "Success! Retrieved product details\n";
echo "Product name: " . ($productDetail['data']['product_name'] ?? 'unknown') . "\n";
} else {
echo "Error: Failed to retrieve product details\n";
echo "Response: " . print_r($productDetail, true) . "\n";
}
}
}
} else {
echo "Error: Invalid catalog response\n";
echo "Response: " . print_r($result, true) . "\n";
}
echo "\nAPI test completed successfully!\n";
} catch (Exception $e) {
echo "ERROR: " . $e->getMessage() . "\n";
if ($e instanceof EcomZoneException) {
echo "Error Code: " . $e->getCode() . "\n";
}
echo "\nStack Trace:\n" . $e->getTraceAsString() . "\n";
}