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

174 lines
5.7 KiB
PHP

<?php
// Script to test API connectivity
// Display errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Define required constants if not defined
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));
}
// Load required classes directly
require_once(dirname(__FILE__) . '/classes/EcomZoneLogger.php');
require_once(dirname(__FILE__) . '/classes/EcomZoneException.php');
require_once(dirname(__FILE__) . '/classes/EcomZoneClient.php');
// Create log directory if it doesn't exist
$logDir = __DIR__ . '/log';
if (!file_exists($logDir)) {
mkdir($logDir, 0755, true);
}
// Mock PrestaShop classes
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' // Actual API token
];
return isset($config[$key]) ? $config[$key] : $default;
}
public static function updateValue($key, $value) {
return true;
}
}
}
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;
}
}
}
// Disable output buffering
if (ob_get_level()) {
ob_end_clean();
}
// Set headers for plain text output
header('Content-Type: text/plain');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
echo "EcomZone API Test\n";
echo "================\n\n";
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 test_api.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 test_api.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";
$start = microtime(true);
$result = $client->getCatalog(1, 1);
$end = microtime(true);
echo "Request completed in " . round(($end - $start) * 1000, 2) . " ms\n";
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";
$start = microtime(true);
$productDetail = $client->getProduct($sku);
$end = microtime(true);
echo "Request completed in " . round(($end - $start) * 1000, 2) . " ms\n";
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";
echo "\nCheck the log file for more details: modules/ecomzone/log/ecomzone.log\n";
}
echo "\nLog contents (last 10 entries):\n";
echo "==============================\n\n";
try {
$logEntries = EcomZoneLogger::getLogContents(10);
foreach ($logEntries as $entry) {
$context = '';
if (!empty($entry['context'])) {
$context = ' - ' . json_encode($entry['context']);
}
echo $entry['timestamp'] . ' [' . $entry['level'] . '] ' . $entry['message'] . $context . "\n";
}
} catch (Exception $e) {
echo "Failed to retrieve logs: " . $e->getMessage() . "\n";
}