Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include "AuthHeader.php";
- include "GetHighResImageRequestType.php";
- require_once "Constants.php";
- require_once "Logging.php";
- function getAuthHeader() {
- $username = "";
- $key = "";
- if( TEST_MODE == true ) {
- $username = USERNAME_TEST;
- $key = KEY_TEST;
- } else {
- $username = USERNAME_LIVE;
- $key = KEY_LIVE;
- }
- $rand = rand( 0,1000000 );
- $objDateTime = new DateTime( 'NOW', new DateTimeZone( "Europe/London" ) );
- $concat = $rand . $objDateTime->format( 'c' ) . $key;
- $pass = base64_encode( sha1( $concat ) );
- $auth = new AuthHeader( $username, $pass, $rand, $objDateTime->format( 'c' ) );
- return $auth;
- }
- function getToken() {
- $postData = array(
- 'username' => USERNAME,
- 'password' => PASSWORD
- );
- $ch = curl_init( URL_TOKEN );
- curl_setopt_array( $ch, array(
- CURLOPT_POST => TRUE,
- CURLOPT_RETURNTRANSFER => TRUE,
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: application/json'
- ),
- CURLOPT_POSTFIELDS => json_encode( $postData ),
- CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_SSL_VERIFYHOST => 0
- ));
- $response = curl_exec( $ch );
- if( $response === FALSE ) {
- die( curl_error( $ch ) );
- }
- $responseData = json_decode( $response, TRUE );
- return $responseData;
- }
- function downloadImage( $sku ) {
- $client = new SoapClient( URL_BEAUTYFORT_SOAP );
- $authHeader = getAuthHeader();
- $GetHighResImageRequestType = new GetHighResImageRequestType( TEST_MODE, $sku, false );
- $GetHighResImageParams = array(
- "GetHighResImageRequestType" => $GetHighResImageRequestType
- );
- $paramsHeader = array( "AuthHeader" => $authHeader );
- $header = new SoapHeader( URL_BEAUTYFORT_API, "AuthHeader", $authHeader );
- $client->__setSoapHeaders( $header );
- $GetHighResImageResponse = $client->__soapCall( "GetHighResImage", $GetHighResImageParams );
- return $GetHighResImageResponse;
- }
- function syncProduct( $token, $prod, $array_cat, $image, $i ) {
- $dateTime = ( new DateTime())->format( 'Y-m-d' );
- $log = new Logging();
- $log->lfile( dirname(__FILE__) . "/log/" . $dateTime . ".txt" );
- try { $qty = 0;
- if( $prod[6] != "" ) {
- if( $prod[6] >= 1 ) {
- $qty = $prod[6];
- } else { $qty = 0; }
- } else if( $prod[7] != "" ) {
- if( $prod[7] >= 1 ) {
- $qty = $prod[7];
- } else { $qty = 0; }
- }
- $upc_ean = explode( ",", $prod[14] );
- if( $upc_ean[0] == "" ) {
- $log->lwrite( $i." no-barcode -> ".$prod[0] );
- $upc_ean[0] = "000000000";
- return;
- }
- $log->lwrite( $i." ok -> ".$prod[0] );
- $postData = array(
- "product" => array(
- "attribute_set_id" => 9,
- "sku" => $prod[0],
- "name" => utf8_encode( $prod[2] ),
- "price" => $prod[15],
- "status" => 1,
- "custom_attributes" => array(
- '0' => array(
- "attribute_code" => "category_ids",
- "value" => array( $array_cat[$prod[16]] ),
- ),
- '1' => array(
- "attribute_code" => "upc_ean",
- "value" => $upc_ean[0],
- ),
- '2' => array(
- "attribute_code" => "brand",
- "value" => utf8_encode( $prod[3] ),
- ),
- '3' => array(
- "attribute_code" => "url_key",
- "value" => $prod[0],
- )
- ),
- "extension_attributes" => array(
- "stock_item" => array(
- "is_in_stock" => 1,
- "qty" => $qty
- )
- )
- ),
- "saveOptions" => true
- );
- if( $image == "" ) {
- if( $prod[12] != "" ) {
- $imageThumb = file_get_contents( $prod[12] );
- if ( $imageThumb !== false ) {
- $imageThumbb64 = base64_encode( $imageThumb );
- }
- $postData['product']['media_gallery_entries'] = array(
- array(
- "mediaType" => "image",
- "position" => 0,
- "disabled" => false,
- "types" => array( "thumbnail" ),
- "content" => array(
- "base64_encoded_data" => $imageThumbb64,
- "type" => "image/jpeg",
- "name" => "img_" . $prod[0] . ".jpg"
- )
- )
- );
- }
- } else {
- $imageThumb = file_get_contents( $image );
- if ( $imageThumb !== false ) {
- $imageThumbb64 = base64_encode( $imageThumb );
- }
- $postData['product']['media_gallery_entries'] = array(
- array(
- "mediaType" => "image",
- "position" => 0,
- "disabled" => false,
- "types" => array( "thumbnail", "small_image", "image" ),
- "content" => array(
- "base64_encoded_data" => $imageThumbb64,
- "type" => "image/jpeg",
- "name" => "img_" . $prod[0] . ".jpg"
- )
- )
- );
- }
- $ch = curl_init( URL_SYNCH_PRODOTTI.$prod[0] );
- curl_setopt_array( $ch, array(
- CURLOPT_CUSTOMREQUEST => "PUT",
- CURLOPT_RETURNTRANSFER => TRUE,
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: application/json',
- 'Authorization: Bearer '.$token,
- ),
- CURLOPT_POSTFIELDS => json_encode( $postData ),
- CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_SSL_VERIFYHOST => 0
- ));
- $response = curl_exec( $ch );
- if( $response === FALSE ) {
- die( curl_error( $ch ) );
- }
- $responseData = json_decode( $response, TRUE );
- if ( array_key_exists( "price", $responseData ) ) {
- $log->lwrite( "dataok" );
- return $responseData;
- } else {
- if( $responseData['message'] == "URL key for specified store already exists." ) {
- $log->lwrite( "Not saved url key error: " .$prod[0] );
- print_r( $responseData );
- return;
- }
- $log->lwrite( $responseData["message"] );
- syncProduct( $token, $prod, $array_cat, $image, $i );
- }
- } catch ( Exception $e ) {
- echo( " Error - ".$e->getCode() );
- $log->lwrite( " Code: ".$e->getCode()." Message: ".$e->getMessage() );
- syncProduct( $token, $prod, $array_cat, $image, $i );
- }
- }
- function getCategroy( $token ) {
- $ch = curl_init( URL_SYNCH_CATEGORIE );
- curl_setopt_array( $ch, array(
- CURLOPT_CUSTOMREQUEST => "GET",
- CURLOPT_RETURNTRANSFER => TRUE,
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: application/json',
- 'Authorization: Bearer '.$token,
- ),
- CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_SSL_VERIFYHOST => 0
- ));
- $response = curl_exec( $ch );
- if( $response === FALSE ) {
- die( curl_error( $ch ) );
- }
- $responseData = json_decode( $response, TRUE );
- return $responseData;
- }
- function syncCategory( $token, $cat ) {
- $postData = array(
- 'category' => array(
- "name" => $cat,
- "is_active" => true
- )
- );
- $ch = curl_init( URL_SYNCH_CATEGORIE );
- curl_setopt_array( $ch, array(
- CURLOPT_CUSTOMREQUEST => "POST",
- CURLOPT_RETURNTRANSFER => TRUE,
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: application/json',
- 'Authorization: Bearer '.$token,
- ),
- CURLOPT_POSTFIELDS => json_encode( $postData ),
- CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_SSL_VERIFYHOST => 0
- ));
- $response = curl_exec( $ch );
- if( $response === FALSE ) {
- die( curl_error( $ch ) );
- }
- $responseData = json_decode( $response, TRUE );
- return $responseData;
- }
- function syncProductRemove( $token, $prod ) {
- $dateTime = ( new DateTime() )->format( 'Y-m-d' );
- $log = new Logging();
- $log->lfile( dirname(__FILE__) . "/log/" . $dateTime . ".txt" );
- try { $qty = 0;
- $postData = array(
- 'product' => array(
- "sku" => $prod[0],
- "status" => 0,
- "extension_attributes" => array(
- "stock_item" => array(
- "is_in_stock" => 0,
- "qty" => 0
- )
- )
- ),
- 'saveOptions' => true
- );
- $ch = curl_init( URL_SYNCH_PRODOTTI.$prod );
- curl_setopt_array( $ch, array(
- CURLOPT_CUSTOMREQUEST => "PUT",
- CURLOPT_RETURNTRANSFER => TRUE,
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: application/json',
- 'Authorization: Bearer '.$token,
- ),
- CURLOPT_POSTFIELDS => json_encode( $postData ),
- CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_SSL_VERIFYHOST => 0
- ));
- $response = curl_exec( $ch );
- if( $response === FALSE ) {
- die( curl_error( $ch ) );
- }
- $responseData = json_decode( $response, TRUE );
- if (array_key_exists( "price", $responseData ) ) {
- return $responseData;
- } else {
- if( $responseData['message'] == "URL key for specified store already exists" ) {
- $log->lwrite( "Not saved url key error: " .$prod );
- print_r( $responseData );
- return;
- }
- $log->lwrite( $responseData["message"] );
- syncProductRemove( $token, $prod );
- }
- } catch ( Exception $e ) {
- echo( " Error - ".$e->getCode() );
- $log->lwrite( " Code: ".$e->getCode()." Message: ".$e->getMessage() );
- syncProductRemove( $token,$prod );
- }
- }
- function getAllProd( $token, $cat_id ) {
- $ch = curl_init( URL_SYNCH_CATEGORIE.$cat_id."/products" );
- curl_setopt_array($ch, array(
- CURLOPT_CUSTOMREQUEST => "GET",
- CURLOPT_RETURNTRANSFER => TRUE,
- CURLOPT_HTTPHEADER => array(
- 'Content-Type: application/json',
- 'Authorization: Bearer '.$token,
- ),
- CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_SSL_VERIFYHOST => 0
- ));
- $response = curl_exec( $ch );
- if( $response === FALSE ) {
- die( curl_error( $ch ) );
- }
- $responseData = json_decode( $response, TRUE );
- return $responseData;
- }
- <?php
- include "GetStockFileRequestType.php";
- require_once "Functions.php";
- $client = new SoapClient( URL_BEAUTYFORT_SOAP );
- $increment = 1.59;
- $dateTime = ( new DateTime() )->format( 'Y-m-d' );
- $log = new Logging();
- $log2 = new Logging();
- $log->lfile( dirname(__FILE__) . "/log/" . $dateTime . ".txt" );
- $log2->lfile( dirname(__FILE__) . "/log/" . $dateTime . "_productsynched.txt" );
- $file = dirname(__FILE__) . "/tmp/" . $dateTime . ".txt";
- if( file_exists( $file ) ) {
- echo( "Exist " );
- $log->lwrite( "Exist " );
- }
- $authHeader = getAuthHeader();
- $GetStockFileRequestType = new GetStockFileRequestType( TEST_MODE, "Delimited", null, null, "StockCode", ";" );
- $GetStockFileParams = array(
- "GetStockFileRequestType" => $GetStockFileRequestType
- );
- $paramsHeader = array( "AuthHeader" => $authHeader );
- $header = new SoapHeader( URL_BEAUTYFORT_API, "AuthHeader" , $authHeader );
- $client->__setSoapHeaders( $header );
- $GetStockFileResponse = $client->__soapCall( "GetStockFile", $GetStockFileParams );
- echo file_put_contents( "BeautyFortCatalog.csv", $GetStockFileResponse->File );
- $Data = str_getcsv( $GetStockFileResponse->File, "n" );
- $i=0;
- $arr_p = array();
- $arr_cat = array();
- foreach( $Data as $Row ) {
- $Row = str_getcsv( $Row, ";" );
- if ( $i != 0 ) {
- $log->lwrite( " -> " . $Row[0] . " - " . $Row[2] );
- $cat = explode( " > ", $Row[1] )[0];
- if( $Row[9] == "" ) {
- $price = $Row[8] * $increment;
- } else {
- $price = $Row[9];
- }
- $Row[15] = round( $price, 2, PHP_ROUND_HALF_UP );
- $Row[16] = $cat;
- if ( !array_key_exists( $cat, $arr_cat ) ) {
- $arr_cat[$cat] = 0;
- }
- } else {
- $Row[15] = "Final Price";
- $Row[16] = "New Category";
- }
- $arr_p[] = $Row;
- $i++;
- }
- $authToken = getToken();
- $cat = getCategroy( $authToken );
- $array_p_magento = array();
- foreach( $arr_cat as $key => $row ) {
- foreach ( $cat["children_data"] as $key2 => $row2 ) {
- if ( $row2["name"] == $key ) {
- $arr_cat[$key] = $row2["id"];
- }
- }
- if ( $arr_cat[$key] == 0 ) {
- $category = syncCategory( $authToken, $key );
- $arr_cat[$key] = $category["id"];
- }
- $products = getAllProd( $authToken, $arr_cat[$key] );
- foreach ( $products as $keyp => $product ) {
- if( $product ) {
- $array_p_magento[] = $product["sku"];
- }
- }
- }
- $file2 = dirname(__FILE__) . "/log/" . $dateTime . "_productsynched.txt";
- $esegui = true;
- $lastProduct = "";
- if( file_exists( $file2 ) ) {
- $esegui = false;
- $Data2 = str_getcsv( file_get_contents( $file2 ), "n" );
- $lastProduct = explode( " ", end( $Data2 ) );
- }
- $i=0;
- foreach( $arr_p as $row ) {
- if ( $i != 0 ) {
- if( $lastProduct != "" ) {
- if( $lastProduct[2] == $row[0] ) {
- $esegui = true;
- }
- }
- if( $row[15] > 1 ) {
- if( $esegui ) {
- $image = $row[13];
- syncProduct( $authToken, $row, $arr_cat, $image, $i );
- $log2->lwrite( $row[0] );
- }
- if( ( $key = array_search( $row[0], $array_p_magento ) ) !== false ) {
- unset( $array_p_magento[$key] );
- }
- }
- }
- $i++;
- }
- foreach( $array_p_magento as $row ) {
- syncProductRemove( $authToken, $row );
- $log->lwrite("Removed - " . $row);
- }
- $log->lwrite( "Done Unlinking File" );
- unlink( $file2 );
- echo( "Done" );
- $log->lwrite( "File Unlinked" );
- $fp = fopen( $file, "wb" );
- fwrite( $fp, $dateTime );
- fclose( $fp );
- $files = glob( dirname(__FILE__) . "/log/*" );
- $now = time();
- foreach ( $files as $file ) {
- if (is_file( $file )) {
- if ($now - filemtime( $file ) >= 60 * 60 * 24 * 2) {
- unlink( $file );
- }
- }
- }
Add Comment
Please, Sign In to add comment