Product.php (Before) Product.php (After)
<?php <?php
   
namespace App\Model\Shopify; namespace App\Model\Shopify;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
* Class Product * Class Product
* @package model\shopify * @package model\shopify
*/  */ 
   
class Product extends Model{ class Product extends Model{
         
   private  $shopifyObj ;    private  $shopifyObj ;
   public $shop_domain;    public $shop_domain;
.    private $shopify_api_version;  
   private $token;    private $token;
   private $api_key;    private $api_key;
   private $secret;    private $secret;
   private $last_response_headers = null;    private $last_response_headers = null;
   protected $global;    protected $global;
   protected $apiCount;    protected $apiCount;
         
   function __construct(){    function __construct(){
   
       $this->api_key  = env('SHOPIFY_API_KEY', true);        $this->api_key  = env('SHOPIFY_API_KEY', true);
       $this->token    = env('SHOPIFY_PASSWORD', true);        $this->token    = env('SHOPIFY_PASSWORD', true);
       $this->secret = env('SHOPIFY_SECRET', true);        $this->secret = env('SHOPIFY_SECRET', true);
       $this->shop_domain   = env('SHOPIFY_DOMAIN', true);        $this->shop_domain   = env('SHOPIFY_DOMAIN', true);
.        $this->shopify_api_version = env('SHOPIFY_API_VERSION', true);  
   
   }    }
   
   function setModel(){    function setModel(){
   
       $this->shopifyObj = new shopify();        $this->shopifyObj = new shopify();
   }    }
         
   /**    /**
    * Getting shopify products based on productID and variantID     * Getting shopify products based on productID and variantID
    */      */ 
    function getShopifyProductByID($productID, $variantID){     function getShopifyProductByID($productID, $variantID){
   
       $method = 'GET';        $method = 'GET';
   
.        $path   = '/admin/api/'.$this->shopify_api_version.'/products/'.$productID.'/variants/'.$variantID.'.json';        $path   = '/admin/products/'.$productID.'/variants/'.$variantID.'.json';
   
       $products = $this->call($method, $path);        $products = $this->call($method, $path);
       if($products){        if($products){
           return  $products;            return  $products;
       }        }
       return null;        return null;
   }    }
   
   /**    /**
    * Getting Shopify shipping zone     * Getting Shopify shipping zone
    */      */ 
   
    function getShopifyShippingZone() {     function getShopifyShippingZone() {
   
       $method = 'GET';        $method = 'GET';
.        $path   = '/admin/api/'.$this->shopify_api_version.'/shipping_zones.json';        $path   = '/admin/shipping_zones.json';
       //$path   = '/admin/api/'.$this->shopify_api_version.'/shipping_zones.json';        //$path   = '/admin/api/'.$this->shopify_api_version.'/shipping_zones.json';
   
       $shipping_zones = $this->call($method, $path);        $shipping_zones = $this->call($method, $path);
       if($shipping_zones){        if($shipping_zones){
           return  $shipping_zones;            return  $shipping_zones;
       }        }
       return null;        return null;
   
    }     }
   
    function call($method, $path, $params=array())     function call($method, $path, $params=array())
   {    {
   
       /****************api limit control**********************************/         /****************api limit control**********************************/ 
       $duration = 1;        $duration = 1;
       $apiLimit = 2;        $apiLimit = 2;
       if($this->global['apiRequestTime'] == 0)        if($this->global['apiRequestTime'] == 0)
       {                {        
           $this->global['apiRequestTime'] = strtotime("now");            $this->global['apiRequestTime'] = strtotime("now");
       }        }
       else{        else{
           $timeDifference = strtotime("now")- $this->global['apiRequestTime'];            $timeDifference = strtotime("now")- $this->global['apiRequestTime'];
           if ($timeDifference< $duration)  {            if ($timeDifference< $duration)  {
               if ( $this->apiCount >= $apiLimit ) {                if ( $this->apiCount >= $apiLimit ) {
                   $sleepTime = (int)ceil($duration-$timeDifference);                    $sleepTime = (int)ceil($duration-$timeDifference);
                   sleep($sleepTime);  // this step will wait for next api request.                    sleep($sleepTime);  // this step will wait for next api request.
                   $this->global['apiRequestTime'] = strtotime("now");                    $this->global['apiRequestTime'] = strtotime("now");
                   $this->apiCount = 0;       // next cycle starts                    $this->apiCount = 0;       // next cycle starts
               }                }
           }else {            }else {
               $this->global['apiRequestTime'] = strtotime("now");                $this->global['apiRequestTime'] = strtotime("now");
               $this->apiCount = 0;                $this->apiCount = 0;
           }            }
       }                }        
       $this->apiCount++;        $this->apiCount++;
                 
          /**************************************************/            /**************************************************/ 
       $baseurl = "https://{$this->shop_domain}/";        $baseurl = "https://{$this->shop_domain}/";
         
       $url = $baseurl.ltrim($path, '/');        $url = $baseurl.ltrim($path, '/');
       $query = in_array($method, array('GET','DELETE')) ? $params : array();        $query = in_array($method, array('GET','DELETE')) ? $params : array();
       $payload = in_array($method, array('POST','PUT')) ? json_encode($params) : array();        $payload = in_array($method, array('POST','PUT')) ? json_encode($params) : array();
       $request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();        $request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
   
       // add auth headers        // add auth headers
       $request_headers[] = 'X-Shopify-Access-Token: ' . $this->token;        $request_headers[] = 'X-Shopify-Access-Token: ' . $this->token;
   
       $response = $this->curlHttpApiRequest($method, $url, $query, $payload, $request_headers);        $response = $this->curlHttpApiRequest($method, $url, $query, $payload, $request_headers);
       $response = json_decode($response, true);        $response = json_decode($response, true);
                 
       return $response;        return $response;
   }    }
   
    function curlHttpApiRequest($method, $url, $query='', $payload='', $request_headers=array())     function curlHttpApiRequest($method, $url, $query='', $payload='', $request_headers=array())
   {    {
   
       $curl = curl_init();        $curl = curl_init();
       curl_setopt($curl, CURLOPT_URL, $url);        curl_setopt($curl, CURLOPT_URL, $url);
       curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($curl, CURLOPT_ENCODING, '');        curl_setopt($curl, CURLOPT_ENCODING, '');
       curl_setopt($curl, CURLOPT_MAXREDIRS, 10);        curl_setopt($curl, CURLOPT_MAXREDIRS, 10);
       curl_setopt($curl, CURLOPT_TIMEOUT, 30);        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
       curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
       curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
   
       if ($method !== 'GET') {        if ($method !== 'GET') {
   
           curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);            curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
       }        }
   
       curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);        curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
           
       $response = curl_exec($curl);        $response = curl_exec($curl);
       return $response;        return $response;
   
   }    }
   
} }