OpenCart 4.0 API Ինտեգրացիա - Մշակողի Ուղեցույց

OpenCart 4.0 API Ինտեգրացիա
OpenCart 4.0-ը բերում է ուժեղ API գործառույթներ, որոնք թույլ են տալիս ինտեգրել կայքը երրորդ կողմի ծառայությունների հետ և ստեղծել հատուկ լուծումներ։ Այս ուղեցույցը կցուցադրի ամբողջական գործընթացը:
API հիմունքներ:
1. Authentication Setup
// API Token generation
$username = "api_user";
$password = "secure_password";
$api_token = hash("sha256", $username . $password . date("Y-m-d H:i:s"));
// Headers կարգավորում
$headers = [
"Content-Type: application/json",
"Authorization: Bearer " . $api_token,
"Accept: application/json"
];
2. Հիմնական endpoints:
/api/customers
- հաճախորդների կառավարում/api/products
- արտադրանքի կառավարում/api/orders
- պատվերների կառավարում/api/categories
- կատեգորիաների կառավարում/api/manufacturers
- արտադրողների կառավարում
Գործնական օրինակներ:
1. Նոր արտադրանք ավելացնել:
function addProduct($product_data, $headers) {
$url = "https://yourstore.com/api/products";
$data = [
"name" => $product_data["name"],
"description" => $product_data["description"],
"price" => $product_data["price"],
"sku" => $product_data["sku"],
"quantity" => $product_data["quantity"],
"status" => 1,
"category_id" => $product_data["category_id"],
"image" => $product_data["image_url"]
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 201) {
return json_decode($response, true);
} else {
throw new Exception("API Error: " . $response);
}
}
2. Պատվերի տվյալների ստացում:
function getOrder($order_id, $headers) {
$url = "https://yourstore.com/api/orders/" . $order_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 200) {
return json_decode($response, true);
} else {
return false;
}
}
Ընդլայնված ֆունկցիոնալություն:
1. Webhook կարգավորում:
// Webhook endpoint handler
function handleOpenCartWebhook() {
$payload = file_get_contents("php://input");
$data = json_decode($payload, true);
// Verify webhook signature
$signature = $_SERVER["HTTP_X_OPENCART_SIGNATURE"] ?? "";
if (!verifyWebhookSignature($payload, $signature)) {
http_response_code(401);
exit("Unauthorized");
}
$event_type = $data["event"] ?? "";
switch ($event_type) {
case "order.created":
processNewOrder($data["order"]);
break;
case "order.updated":
updateOrderStatus($data["order"]);
break;
case "product.updated":
syncProductToWarehouse($data["product"]);
break;
case "customer.registered":
sendWelcomeEmail($data["customer"]);
break;
default:
error_log("Unknown webhook event: " . $event_type);
}
http_response_code(200);
echo "OK";
}
2. Inventory sync բազմաքանակ արտադրանքների համար:
class InventorySync {
private $api_url;
private $headers;
public function __construct($api_url, $headers) {
$this->api_url = $api_url;
$this->headers = $headers;
}
public function syncBulkInventory($products) {
$batch_size = 50;
$batches = array_chunk($products, $batch_size);
foreach ($batches as $batch) {
$this->processBatch($batch);
sleep(1); // Rate limiting
}
}
}
Անվտանգության նկատառումներ:
✅ Best Practices:
- HTTPS միայն օգտագործել
- API keys-ը environment variables-ում պահել
- Input validation բոլոր տվյալների համար
- SQL injection պաշտպանություն
- Rate limiting իմպլեմենտել
- Comprehensive logging ավելացնել
- Error handling բոլոր requests-ների համար
Սխալների մշակում:
function handleApiError($response_code, $response_body) {
switch ($response_code) {
case 401:
throw new UnauthorizedException("Invalid API credentials");
case 403:
throw new ForbiddenException("Access denied");
case 404:
throw new NotFoundException("Resource not found");
case 429:
throw new RateLimitException("Rate limit exceeded");
case 500:
throw new ServerException("Internal server error: " . $response_body);
default:
if ($response_code >= 400) {
throw new ApiException("API Error ($response_code): " . $response_body);
}
return json_decode($response_body, true);
}
}
Թեստավորում և դեբագինգ:
🔧 Թեստավորման գործիքներ:
- Postman/Insomnia օգտագործել API calls-ի թեստման համար
- Unit tests գրել բոլոր API functions-ի համար
- Integration tests ստեղծել end-to-end scenarios-ի համար
- Log monitoring կարգավորել production environment-ում
- Performance metrics հավաքել API response times-ի համար
Եզրակացություն: API-ի ճիշտ օգտագործումը և բարձրորակ ինտեգրացիան կարող են զգալիորեն բարելավել ձեր OpenCart խանութի գործառույթները և ստեղծել արժեքավոր բիզնես լուծումներ: