<?php

include_once 'inc/functions.php';

include_once 'inc/database.php';
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

$db1=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);

$res=mysqli_query($db1,"SELECT * FROM stores");

while($row=mysqli_fetch_assoc($res))
{

  $store_hash=$row["store_id"];
  $access_token=$row["access_token"];

  $curl = curl_init();

  curl_setopt_array($curl, [
    CURLOPT_URL => "https://api.bigcommerce.com/stores/".$store_hash."/v2/store",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => [
      "Accept: application/json",
      "Content-Type: application/json",
      "X-Auth-Token:".$access_token
    ],
  ]);

  $response = curl_exec($curl);
  $err = curl_error($curl);

  curl_close($curl);

  if ($err) {
    echo "cURL Error #:" . $err;
  } else {

    echo $response;
    $store_info=json_decode($response,true);

    echo "<pre>";
    print_r($store_info);
    echo "<pre>";

    if(isset($store_info["account_uuid"]))
    {
      $store_uuid=$store_info["account_uuid"];
      echo $store_uuid;

      mysqli_query($db1,"UPDATE stores SET merchnat_account_uuid='$store_uuid' WHERE store_id='$store_hash'");

    }
  }
}

?>