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; } }