Fix image upload structure for Miravia API compliance

🔧 Bug Fixes:
- Fixed product image structure to match Miravia API requirements
- Updated MiraviaProduct.php getData() method to wrap images in {"Image": [...]} format
- Updated MiraviaCombination.php getData() method to wrap SKU images properly
- Resolved error "[4224] The Main image of the product is required"

📋 Changes:
- Modified getData() methods to transform flat image arrays to nested structure
- Product images: images[] → Images: {"Image": [...]}
- SKU images: images[] → Images: {"Image": [...]}
- Maintains backward compatibility for empty image arrays

🎯 Impact:
- Product uploads will now pass Miravia's image validation
- Both product-level and SKU-level images properly formatted
- Complies with official Miravia API documentation structure

🤖 Generated with Claude Code (https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Miravia Connector Bot
2025-07-21 13:57:16 +02:00
parent 191af6b0f8
commit 552bce9f84
7 changed files with 1224 additions and 39 deletions

View File

@@ -38,6 +38,107 @@ class MiraviaSdk
}
}
/**
* Submit feed with multiple products
*/
public function submitFeed($feedData)
{
if(!$this->client || empty($this->access_token)) {
$this->last_error = 'SDK not configured properly';
return false;
}
try {
// Step 1: Create feed document
$feedDocumentRequest = new SimpleIopRequest('/feed/createFeedDocument');
$feedDocumentRequest->addApiParam('content_type', 'application/json');
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Step 1 - Creating feed document");
}
$feedDocResponse = $this->client->execute($feedDocumentRequest, $this->access_token);
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Feed document response: " . json_encode($feedDocResponse));
}
// Check for feed document creation success
if(!isset($feedDocResponse->feed_result) || !$feedDocResponse->feed_result->success) {
$this->last_error = 'Failed to create feed document';
return false;
}
$feedDocumentId = $feedDocResponse->feed_result->result->feed_document_id;
$uploadUrl = $feedDocResponse->feed_result->result->url;
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Feed document created - ID: $feedDocumentId");
}
// Step 2: Upload feed data to the feed document URL
$uploadSuccess = $this->uploadFeedDocument($uploadUrl, json_encode($feedData));
if(!$uploadSuccess) {
$this->last_error = 'Failed to upload feed data to feed document';
return false;
}
// Step 3: Create feed
$createFeedRequest = new SimpleIopRequest('/feed/createFeed');
$createFeedRequest->addApiParam('feed_type', 'PRODUCT_LISTING');
$createFeedRequest->addApiParam('feed_document_id', $feedDocumentId);
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Step 3 - Creating feed for processing");
}
$createFeedResponse = $this->client->execute($createFeedRequest, $this->access_token);
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Create feed response: " . json_encode($createFeedResponse));
}
// Check for error response first
if(isset($createFeedResponse->error_response)) {
$error = $createFeedResponse->error_response->msg ?? 'Unknown API error';
$error_code = $createFeedResponse->error_response->code ?? 'NO_CODE';
$this->last_error = "API Error ({$error_code}): {$error}";
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: API Error - Code: {$error_code}, Message: {$error}");
}
return false;
}
// Check for successful feed creation
if(isset($createFeedResponse->feed_result) && $createFeedResponse->feed_result->success) {
$feedId = $createFeedResponse->feed_result->result;
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Feed created successfully - Feed ID: $feedId");
}
return [
'success' => true,
'feed_id' => $feedId,
'feed_document_id' => $feedDocumentId,
'message' => 'Feed submitted successfully'
];
} else {
$this->last_error = 'Failed to create feed';
return false;
}
} catch (Exception $e) {
$this->last_error = $e->getMessage();
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Exception: " . $e->getMessage());
}
return false;
}
}
/**
* Create/Upload products to Miravia using proper API format
*/
@@ -625,4 +726,98 @@ class MiraviaSdk
return false;
}
}
/**
* Get feed results document content
*/
public function getFeedResults($feedDocumentId)
{
if(!$this->client || empty($this->access_token)) {
$this->last_error = 'SDK not configured properly';
return false;
}
try {
$request = new SimpleIopRequest('/feed/getFeedDocument');
$request->addApiParam('feed_document_id', $feedDocumentId);
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Getting feed results for document ID: $feedDocumentId");
}
$response = $this->client->execute($request, $this->access_token);
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Feed results response: " . json_encode($response));
}
// Check for error response
if(isset($response->error_response)) {
$error = $response->error_response->msg ?? 'Unknown API error';
$error_code = $response->error_response->code ?? 'NO_CODE';
$this->last_error = "API Error ({$error_code}): {$error}";
return false;
}
// Check for successful response
if(isset($response->feed_result) && $response->feed_result->success) {
$documentUrl = $response->feed_result->result->url;
// Download the results document
$resultsContent = $this->downloadFeedDocument($documentUrl);
if($resultsContent) {
return json_decode($resultsContent, true);
}
}
return false;
} catch (Exception $e) {
$this->last_error = $e->getMessage();
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Exception: " . $e->getMessage());
}
return false;
}
}
/**
* Download feed document content from URL
*/
private function downloadFeedDocument($documentUrl)
{
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Downloading feed document from: " . substr($documentUrl, 0, 50) . "...");
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $documentUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if(class_exists('LOG')) {
LOG::add("DEBUG SDK: Download response code: " . $httpCode);
if($error) {
LOG::add("DEBUG SDK: Download error: " . $error);
}
}
if($httpCode === 200 && !$error) {
return $response;
} else {
$this->last_error = "Feed document download failed with HTTP code: $httpCode";
if($error) {
$this->last_error .= " - $error";
}
return false;
}
}
}