document.write("
define( 'STORE_CODE', 'dev' ); define( 'API_URL', 'https://localhost/mm5/json.mvc' ); define( 'API_TOKEN', '64d39340a4aaf6e3c752a461f68be2d2' ); define( 'API_SIGNING_KEY', 'NglO1/NYZeYmnID0DrMIjuXqdD3nxBpNLp0FJlfjQbQ' ); // this is a base64 encoded string - copy directly from Miva Admin define( 'API_SIGNING_DIGEST', 'sha256' ); // sha256, sha1, or null. sha256 is the default setting. $request = [ 'Store_Code' => STORE_CODE, 'Function' => 'ProductList_Load_Query', 'Miva_Request_Timestamp' => time() ]; try { $content = json_encode( $request ); $response = sendAPICurlRequest( $content ); } catch ( Exception $e ) { die( $e->getMessage() ); } /** * Sends an api curl request. */ function sendAPICurlRequest( $content ) { $handle = curl_init( API_URL ); if ( !is_resource( $handle ) ) { throw new Exception( 'Curl Error: Unable to initialize' ); } $curlOptions = [ CURLOPT_POST => true, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_SSL_VERIFYHOST => 2 //CURLOPT_SSL_VERIFYPEER => false, //CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_POSTFIELDS => $content, CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', sprintf( 'X-Miva-API-Authorization: %s', generateAuthHeader( API_TOKEN, API_SIGNING_KEY, API_SIGNING_DIGEST, $content ) ) ] ]; if ( curl_setopt_array( $handle, $curlOptions ) === false ) { $error = curl_error( $handle ); $errorno = curl_errno( $handle ); curl_close($handle); throw new Exception( sprintf( 'Error Setting Curl Options', $error, $errorno ) ); } $response = curl_exec( $handle ); if ( curl_errno( $handle ) !== 0 ) { $error = curl_error( $handle ); $errorno = curl_errno( $handle ); curl_close( $handle ); throw new Exception( sprintf( 'HTTP Error: %s Code %d', $error, $errorno ) ); } curl_close( $handle ); return $response; } /** * Generate the authentication header value */ function generateAuthHeader( $apiToken, $signingKey, $digest, $data ) { if ( !in_array( $digest, ['sha256','sha1'] ) ) { return sprintf( 'MIVA %s', $apiToken ); } $signature = hash_hmac( $digest, $data, base64_decode( $signingKey ), true ); if ( $signature === false ) { throw new Exception( 'Error generating signature' ); } return sprintf( 'MIVA-HMAC-%s %s:%s', strtoupper( $digest ), $apiToken, base64_encode( $signature ) ); }