🔧 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>
244 lines
7.5 KiB
PHP
244 lines
7.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Simplified IOP Client for WordPress plugin
|
|
* Based on AliExpress SDK but without namespaces
|
|
*/
|
|
|
|
class SimpleIopClient
|
|
{
|
|
public $appKey;
|
|
public $secretKey;
|
|
public $gatewayUrl;
|
|
protected $signMethod = "sha256";
|
|
protected $sdkVersion = "iop-sdk-php-20220608";
|
|
public $last_error = '';
|
|
|
|
public function __construct($url = "", $appKey = "", $secretKey = "")
|
|
{
|
|
if (strlen($url) === 0) {
|
|
throw new InvalidArgumentException("url is empty", 0);
|
|
}
|
|
$this->gatewayUrl = $url;
|
|
$this->appKey = $appKey;
|
|
$this->secretKey = $secretKey;
|
|
}
|
|
|
|
protected function generateSign($apiName, $params)
|
|
{
|
|
ksort($params);
|
|
$stringToBeSigned = $apiName;
|
|
foreach ($params as $k => $v) {
|
|
if (is_array($v) || is_object($v)) {
|
|
$v = json_encode($v);
|
|
}
|
|
$stringToBeSigned .= $k . $v;
|
|
}
|
|
|
|
return strtoupper(hash_hmac($this->signMethod, $stringToBeSigned, $this->secretKey));
|
|
}
|
|
|
|
public function execute($request, $accessToken = null)
|
|
{
|
|
$apiName = $request->getApiName();
|
|
|
|
// Check if this is a Miravia API endpoint (starts with /)
|
|
if (strpos($apiName, '/') === 0) {
|
|
return $this->executeMiraviaAPI($request, $accessToken);
|
|
} else {
|
|
return $this->executeAliExpressAPI($request, $accessToken);
|
|
}
|
|
}
|
|
|
|
private function executeMiraviaAPI($request, $accessToken = null)
|
|
{
|
|
$apiPath = $request->getApiName();
|
|
$apiParams = $request->getApiParams();
|
|
|
|
// For Miravia Feed API, we need to use the AliExpress-style authentication but with path endpoints
|
|
$sysParams = [
|
|
"app_key" => $this->appKey,
|
|
"sign_method" => $this->signMethod,
|
|
"timestamp" => time() * 1000,
|
|
"partner_id" => $this->sdkVersion,
|
|
"v" => "1.0",
|
|
"format" => "json"
|
|
];
|
|
|
|
if ($accessToken) {
|
|
$sysParams["access_token"] = $accessToken;
|
|
}
|
|
|
|
// Merge API params with system params
|
|
$allParams = array_merge($apiParams, $sysParams);
|
|
|
|
// Generate signature using the API path
|
|
$sign = $this->generateSign($apiPath, $allParams);
|
|
$allParams["sign"] = $sign;
|
|
|
|
$requestUrl = $this->gatewayUrl;
|
|
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SimpleSDK: Making Miravia Feed API request to: " . $requestUrl);
|
|
LOG::add("DEBUG SimpleSDK: API Path: " . $apiPath);
|
|
LOG::add("DEBUG SimpleSDK: Params: " . json_encode($allParams));
|
|
}
|
|
|
|
// Use form POST for Miravia Feed API
|
|
$response = $this->curl($requestUrl, $allParams);
|
|
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SimpleSDK: Miravia Response: " . $response);
|
|
}
|
|
|
|
return json_decode($response);
|
|
}
|
|
|
|
private function executeAliExpressAPI($request, $accessToken = null)
|
|
{
|
|
$sysParams = [
|
|
"app_key" => $this->appKey,
|
|
"sign_method" => $this->signMethod,
|
|
"timestamp" => time() * 1000,
|
|
"partner_id" => $this->sdkVersion,
|
|
"method" => $request->getApiName(),
|
|
"v" => "1.0",
|
|
"format" => "json"
|
|
];
|
|
|
|
if ($accessToken) {
|
|
$sysParams["access_token"] = $accessToken;
|
|
}
|
|
|
|
$apiParams = $request->getApiParams();
|
|
$totalParams = array_merge($apiParams, $sysParams);
|
|
$sign = $this->generateSign($request->getApiName(), $totalParams);
|
|
$totalParams["sign"] = $sign;
|
|
|
|
$requestUrl = $this->gatewayUrl;
|
|
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SimpleSDK: Making AliExpress API request to: " . $requestUrl);
|
|
LOG::add("DEBUG SimpleSDK: Method: " . $request->getApiName());
|
|
LOG::add("DEBUG SimpleSDK: Params: " . json_encode($totalParams));
|
|
}
|
|
|
|
$response = $this->curl($requestUrl, $totalParams);
|
|
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SimpleSDK: AliExpress Response: " . $response);
|
|
}
|
|
|
|
return json_decode($response);
|
|
}
|
|
|
|
private function curl($url, $postFields = null)
|
|
{
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_FAILONERROR, false);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
if (is_array($postFields) && 0 < count($postFields)) {
|
|
$postBodyString = "";
|
|
foreach ($postFields as $k => $v) {
|
|
$postBodyString .= "$k=" . urlencode($v) . "&";
|
|
}
|
|
unset($k, $v);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
|
|
}
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if (curl_errno($ch)) {
|
|
$this->last_error = curl_error($ch);
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SimpleSDK: CURL Error: " . $this->last_error);
|
|
}
|
|
}
|
|
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
|
|
private function curlMiravia($url, $data = null, $accessToken = null)
|
|
{
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_FAILONERROR, false);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
// Set headers for Miravia API
|
|
$headers = [
|
|
'Content-Type: application/json',
|
|
'Accept: application/json'
|
|
];
|
|
|
|
// Add authorization header if token is provided
|
|
if ($accessToken) {
|
|
$headers[] = 'Authorization: Bearer ' . $accessToken;
|
|
}
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
// Send data as JSON
|
|
if ($data) {
|
|
$jsonData = json_encode($data);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
|
|
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SimpleSDK: Sending JSON data: " . $jsonData);
|
|
}
|
|
}
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if (curl_errno($ch)) {
|
|
$this->last_error = curl_error($ch);
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SimpleSDK: CURL Error: " . $this->last_error);
|
|
}
|
|
}
|
|
|
|
if(class_exists('LOG')) {
|
|
LOG::add("DEBUG SimpleSDK: HTTP Response Code: " . $httpCode);
|
|
}
|
|
|
|
curl_close($ch);
|
|
return $response;
|
|
}
|
|
}
|
|
|
|
class SimpleIopRequest
|
|
{
|
|
private $apiName;
|
|
private $apiParams = [];
|
|
|
|
public function __construct($apiName)
|
|
{
|
|
$this->apiName = $apiName;
|
|
}
|
|
|
|
public function addApiParam($key, $value)
|
|
{
|
|
$this->apiParams[$key] = $value;
|
|
}
|
|
|
|
public function getApiName()
|
|
{
|
|
return $this->apiName;
|
|
}
|
|
|
|
public function getApiParams()
|
|
{
|
|
return $this->apiParams;
|
|
}
|
|
} |