<?php

// $listingId = '1670218044';

// $shopifyToken = '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';
// $shopifyShopUrl = 'edadca-bd.myshopify.com'; 
// $shopifyProductId='10136864588107';

// $data = getProduct("https://openapi.etsy.com/v3/application/listings/$listingId", $apiKey);


// $images = image();
// $shopifyProductImagesData = prepareShopifyImageData($shopifyProductId, $images);


// foreach ($shopifyProductImagesData as $imageData) {
//     $result = updateProductImage($shopifyProductId, $imageData['src'], $shopifyToken, $shopifyShopUrl);
//     print_r($result); 
// }


// function getProduct($apiUrl, $apiKey) {
//     $curl = curl_init();
//     curl_setopt($curl, CURLOPT_URL, $apiUrl);
//     curl_setopt($curl, CURLOPT_HTTPHEADER, ['x-api-key: ' . $apiKey, "Content-Type: application/json"]);
//     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//     $response = curl_exec($curl);
//     if ($response === false) {
//         $error = curl_error($curl);
//         curl_close($curl);
//         die("cURL Error: $error");
//     }
//     curl_close($curl);
//     return json_decode($response, true);
// }

// function image() {
//     global $listingId, $apiKey;
//     $apiUrl = "https://openapi.etsy.com/v3/application/listings/$listingId/images";

//     $curl = curl_init();
//     curl_setopt_array($curl, [
//         CURLOPT_URL => $apiUrl,
//         CURLOPT_RETURNTRANSFER => true,
//         CURLOPT_CUSTOMREQUEST => 'GET',
//         CURLOPT_HTTPHEADER => [
//             "x-api-key: $apiKey",
//             "Content-Type: application/json; charset=utf-8"
//         ]
//     ]);

//     $response = curl_exec($curl);
//     if ($response === false) {
//         $error = curl_error($curl);
//         curl_close($curl);
//         die("cURL Error: $error");
//     }

//     curl_close($curl);
//     return json_decode($response, true)['results'];
// }

// function prepareShopifyImageData($productId, $images) {
//     $shopifyProductImagesData = [];
//     foreach ($images as $image) {
//         $shopifyProductImagesData[] = [
//             'product_id' => $productId,
//             'src' => $image['url_fullxfull']
//         ];
//     }
//     return $shopifyProductImagesData;
// }


// function updateProductImage($productId, $imageUrl, $token, $shopUrl) {
//     $imageData = [
//         'image' => [
//             'src' => $imageUrl
//         ]
//     ];

//     $url = "https://$shopUrl/admin/api/2024-07/products/$productId/images.json";

//     $curl = curl_init();
//     curl_setopt($curl, CURLOPT_URL, $url);
//     curl_setopt($curl, CURLOPT_HTTPHEADER, [
//         'X-Shopify-Access-Token: ' . $token,
//         'Content-Type: application/json'
//     ]);
//     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($curl, CURLOPT_POST, true);
//     curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($imageData));

//     $response = curl_exec($curl);
//     if ($response === false) {
//         $error = curl_error($curl);
//         curl_close($curl);
//         die("cURL Error: $error");
//     }

//     $result = json_decode($response, true);
//     curl_close($curl);

//     return $result;
// }

// $product_id = '10136864588107';
// $images = image();

// $addedImages = [];
// foreach ($images as $i) {
//     $addedImage = addProductImage($product_id, $i['listing_image_id'], $i['url_fullxfull']);
//     $addedImages[] = $addedImage;
// }

// $variations = getVariants($product_id);

// // Check if the number of images and variations match

//     $i = 0;
//     foreach ($variations as $variation) {
//         updateVariant($product_id, $addedImages[$i++], $variation['id']);
//     }


// function updateVariant($product_id, $image_id, $variantId) {
//     $token = "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";
//     $shopurl = "edadca-bd.myshopify.com";

//     $data = [
//         'variant' => [
//             'image_id' => $image_id
//         ]
//     ];

//     $json_data = json_encode($data);

//     $api_url = "https://$shopurl/admin/api/2023-04/variants/$variantId.json";

//     $curl = curl_init();
//     curl_setopt($curl, CURLOPT_URL, $api_url);
//     curl_setopt($curl, CURLOPT_HTTPHEADER, array(
//         'X-Shopify-Access-Token: ' . $token,
//         'Content-Type: application/json'
//     ));
//     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
//     curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);

//     $response = curl_exec($curl);
//     curl_close($curl);

//     $data = json_decode($response, true);

//     if (isset($data['errors'])) {
//         echo "Error: " . print_r($data['errors'], true) . "\n";
//     } else {
//         echo "Updated Product Variant:\n";
//         print_r($data['variant']);
//     }
// }

// function getVariants($product_id) {
//     $token = "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";
//     $shopurl = "edadca-bd.myshopify.com";
//     $url = "https://$shopurl/admin/api/2024-07/products/$product_id/variants.json";

//     $curl = curl_init();
//     curl_setopt($curl, CURLOPT_URL, $url);
//     curl_setopt($curl, CURLOPT_HTTPHEADER, array(
//         'X-Shopify-Access-Token: ' . $token,
//         'Content-Type: application/json; charset=utf-8'
//     ));
//     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
//     $response = curl_exec($curl);
//     curl_close($curl);

//     $data = json_decode($response, true);

//     return $data['variants'];
// }

// function image() {
//     $listingId = '1670218044';
//     $apiUrl = "https://openapi.etsy.com/v3/application/listings/$listingId/images";
//     $apiKey = '9vnyhcjjd9p6q4m239g9brio';
//     $curl = curl_init();
//     curl_setopt_array($curl, array(
//         CURLOPT_URL => $apiUrl,
//         CURLOPT_RETURNTRANSFER => true,
//         CURLOPT_CUSTOMREQUEST => 'GET',
//         CURLOPT_HTTPHEADER => array(
//             "x-api-key: $apiKey",
//             "Content-Type: application/json; charset=utf-8"
//         )
//     ));
//     $response = curl_exec($curl);
//     curl_close($curl);
//     $data = json_decode($response, true);
//     return $data['results'];
// }

// function addProductImage($product_id, $id, $image_url) {
//     $token = "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";
//     $shopUrl = "edadca-bd.myshopify.com";
//     $imageData = [
//         'image' => [
//             'id' => $id,
//             'src' => $image_url
//         ]
//     ];

//     $url = "https://$shopUrl/admin/api/2024-07/products/$product_id/images.json";

//     $curl = curl_init();
//     curl_setopt($curl, CURLOPT_URL, $url);
//     curl_setopt($curl, CURLOPT_HTTPHEADER, array(
//         'X-Shopify-Access-Token: ' . $token,
//         'Content-Type: application/json'
//     ));
//     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($curl, CURLOPT_POST, true);
//     curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($imageData));

//     $response = curl_exec($curl);
//     if ($response === false) {
//         $error = curl_error($curl);
//         curl_close($curl);
//         die("cURL Error: $error");
//     }

//     $newImage = json_decode($response, true);
//     curl_close($curl);

//     $id = $newImage['image'];
//     return $id['id'];
// }
// ------------------+--------------------------------------------------------------------+
// | 10110109548875   | Tunbridge Wells Landmarks & Culture: Premium Typographic Art Print |
// | 10107398029643   | Callander Landmarks & Culture: Premium Typographic Art Print       |
// | 10135628972363   | Roath Landmarks & Culture: Premium Typographic Art Print           |
// | 10142022140235   | Stamford Landmarks & Culture: Premium Typographic Art Print        |
// | 10142022369611   | Dinas Powys Landmarks & Culture: Premium Typographic Art Print     |
// | 10142022664523   | Hucknall Landmarks & Culture: Premium Typographic Art Print        |
// | 10142022893899   | Felixstowe Landmarks & Culture: Premium Typographic Art Print      |
// | 10142023024971   | Milford Haven Landmarks & Culture: Premium Typographic Art Print   |
// | 10136864588107   | Ilkley Landmarks & Culture: Premium Typographic Art Print          |
// | 10141993926987   | Cheltenham Landmarks & Culture: Premium Typographic Art Print      |
// +------------------+--------------------------------------------------------------------+

// // //10110109548875
// $token = "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";
//   $shopUrl = "edadca-bd.myshopify.com";   
//   $shopifyproductid=10110109548875 ;
// $data = array("product" => array("id" => $shopifyproductid, "images" => array()));


// print_r($data);
// updateShopifyProduct($token, $shopUrl, $shopifyproductid, $data);


function updateShopifyProduct($token, $shopUrl, $product_id, $data){	
    // $url = "https://".$shopurl."/admin/api/2023-10/products/".$product_id.".json";	  
     $url = "https://".$shopUrl."/admin/api/2024-07/products/".$product_id.".json";
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-Shopify-Access-Token:'.$token, 'Content-Type: application/json; charset=utf-8'));
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($curl, CURLOPT_VERBOSE, 0);
     curl_setopt($curl, CURLOPT_HEADER, 1);
     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
     curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
     $response = curl_exec ($curl);	
     echo $response;
     curl_close ($curl);	
     $response_arr=explode("\n",$response);		

     print_r(  $response);
     $climit = -1;
     foreach($response_arr as $obj){
         if (strpos($obj, 'X-Shopify-Shop-Api-Call-Limit') !== false) {
             $tempArr = explode(":", $obj);
             $climit = substr(trim(end($tempArr)), 0, -3);
         }
     }	
     if(intval($climit) > 35){
         sleep(5);
     }
     if( (strstr(($response_arr[0]), "200")) || (strstr(($response_arr[1]), "200")) || (strstr(($response_arr[2]), "200")) ){
         
         return 1;
     } else {
         echo $product_id."<br />";
        		
         return null;
     }
 }


// $variationImages = getVariationImage();
// $product_id = '10141993926987';
// $images = image();
// // print_r($images);


// foreach ($images as $i) {
//     $addedImage = addProductImage($product_id, $i['url_fullxfull']);
//         foreach($variationImages as $key =>$value){
//             if($value == $i['listing_image_id']){
//                 $variationImages[$key]=$addedImage;
//             }
//         }
// }
// print_r($variationImages);
// $variations = getVariants($product_id);


// print_r($variations);


//     foreach ($variations as $variation) {
       
//         $imageId = $variationImages[$variation['option1']];
//         print_r($imageId);
//         updateVariant($product_id, $imageId, $variation['id']);
//     }


// function getVariationImage(){
//     $apiKey = '9vnyhcjjd9p6q4m239g9brio'; 
// $shopId = '50273658'; 
// $listingId = '1669589014'; 


// $ch = curl_init();


// curl_setopt($ch, CURLOPT_URL, "https://openapi.etsy.com/v3/application/shops/$shopId/listings/$listingId/variation-images");
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HTTPHEADER, [
//     "x-api-key: $apiKey"
// ]);


// $response = curl_exec($ch);


// if(curl_errno($ch)) {
//     echo 'Curl error: ' . curl_error($ch);
//     exit;
// }


// curl_close($ch);


// $responseData = json_decode($response, true);


// echo "<pre>";
// print_r($responseData);
// echo "</pre>";
// $convertedArray = [];
// foreach ($responseData['results'] as $item) {
//     $convertedArray[$item['value']] = $item['image_id'];
// }


// echo "<pre>";
// print_r($convertedArray);
// echo "</pre>";
// return $convertedArray;
// }

// function updateVariant($product_id, $image_id, $variantId) {
//     $token = "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";
//     $shopurl = "edadca-bd.myshopify.com";

//     $data = [
//         'variant' => [
//             'image_id' => $image_id
//         ]
//     ];

//     $json_data = json_encode($data);

//     $api_url = "https://$shopurl/admin/api/2023-04/variants/$variantId.json";

//     $curl = curl_init();
//     curl_setopt($curl, CURLOPT_URL, $api_url);
//     curl_setopt($curl, CURLOPT_HTTPHEADER, array(
//         'X-Shopify-Access-Token: ' . $token,
//         'Content-Type: application/json'
//     ));
//     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
//     curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);

//     $response = curl_exec($curl);
//     curl_close($curl);

//     $data = json_decode($response, true);

//     if (isset($data['errors'])) {
//         echo "Error: " . print_r($data['errors'], true) . "\n";
//     } else {
//         echo "Updated Product Variant:\n";
//         print_r($data['variant']);
//     }
// }

// function getVariants($product_id) {
//     $token = "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";
//     $shopurl = "edadca-bd.myshopify.com";
//     $url = "https://$shopurl/admin/api/2024-07/products/$product_id/variants.json";

//     $curl = curl_init();
//     curl_setopt($curl, CURLOPT_URL, $url);
//     curl_setopt($curl, CURLOPT_HTTPHEADER, array(
//         'X-Shopify-Access-Token: ' . $token,
//         'Content-Type: application/json; charset=utf-8'
//     ));
//     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
//     $response = curl_exec($curl);
//     curl_close($curl);

//     $data = json_decode($response, true);

//     return $data['variants'];
// }

// function image() {
//     $listingId = '1669589014';
//     $apiUrl = "https://openapi.etsy.com/v3/application/listings/$listingId/images";
//     $apiKey = '9vnyhcjjd9p6q4m239g9brio';
//     $curl = curl_init();
//     curl_setopt_array($curl, array(
//         CURLOPT_URL => $apiUrl,
//         CURLOPT_RETURNTRANSFER => true,
//         CURLOPT_CUSTOMREQUEST => 'GET',
//         CURLOPT_HTTPHEADER => array(
//             "x-api-key: $apiKey",
//             "Content-Type: application/json; charset=utf-8"
//         )
//     ));
//     $response = curl_exec($curl);
//     curl_close($curl);
//     $data = json_decode($response, true);
//     return $data['results'];
// }


// function addProductImage($product_id, $image_url) {
//     $token = "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";
//     $shopUrl = "edadca-bd.myshopify.com";
//     $imageData = [
//         'image' => [
//             'src' => $image_url
//         ]
//     ];

//     $url = "https://$shopUrl/admin/api/2024-07/products/$product_id/images.json";

//     $curl = curl_init();
//     curl_setopt($curl, CURLOPT_URL, $url);
//     curl_setopt($curl, CURLOPT_HTTPHEADER, array(
//         'X-Shopify-Access-Token: ' . $token,
//         'Content-Type: application/json'
//     ));
//     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($curl, CURLOPT_POST, true);
//     curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($imageData));

//     $response = curl_exec($curl);
//     if ($response === false) {
//         $error = curl_error($curl);
//         curl_close($curl);
//         die("cURL Error: $error");
//     }

//     $newImage = json_decode($response, true);
//     curl_close($curl);

//     $id = $newImage['image'];
//     return $id['id'];
// }

?>





