app = $app; $this->shopId = $shopId; $this->apiUrl = rtrim($apiUrl, '/') . '/'; $this->token = $token; } protected function throttling(){ if(empty(self::$requestcount[$this->shopId])) { self::$requestcount[$this->shopId] = 1; }else{ self::$requestcount[$this->shopId]++; } if(self::$requestcount[$this->shopId] >= 30) { sleep(2); self::$requestcount[$this->shopId]-=2; }elseif(self::$requestcount[$this->shopId] >= 20) { sleep(2); self::$requestcount[$this->shopId]-=2; } } /** * @param $path * @param string $anweisung * @param string $data * @param bool $returnFormated * * @return array */ public function call($path, $anweisung = '', $data = '', $returnFormated = true, $options = null) { $this->throttling(); $headers = []; $url = $this->apiUrl.'admin/api/'.$this->apiVersion.'/'.$path; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); $header = ['Content-Type: application/json; charset=utf-8']; if(!empty($this->token)) { $header[] = 'X-Shopify-Access-Token: '.$this->token; } curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_VERBOSE, 0); if(!empty($anweisung)){ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $anweisung); } if(!empty($data)){ curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data, $options)); } curl_setopt($curl, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$headers) { $len = strlen($header); $header = explode(':', $header, 2); if ((!empty($header)?count($header):0) < 2){ // ignore invalid headers return $len; } $headers[strtolower(trim($header[0]))][] = trim($header[1]); return $len; } ); $response = curl_exec ($curl); $httpcode = curl_getinfo($curl); $httpcode = (int)$httpcode['http_code']; if($httpcode === 429) { sleep(5); self::$requestcount[$this->shopId] = 40; return $this->call($path, $anweisung, $data); } curl_close ($curl); if(!empty($this->app)){ if(!empty($response['errors']) && preg_match_all('/\This action requires merchant approval for ([a-zA-Z\_]+) scope/',$response['errors'], $erg) && !empty($erg[1])) { $query = sprintf("INSERT INTO `shopexport_log` (`shopid`, `typ`,`parameter1`,`parameter2`,`bearbeiter`,`zeitstempel`) VALUES ('%s','fehler','Fehlendes API-Recht: %s','%s','%s',now())", $this->shopId, $erg[1][0], $response['errors'], $this->app->erp->GetBearbeiter(true) ); $this->app->DB->Insert($query); } } $links = []; if(!empty($headers['link'])){ foreach ($headers['link'] as $link) { $link = str_replace(' ', '', $link); $linkData = explode(',', $link); foreach ($linkData as $linkwithInfo) { $linkParts = explode(';', $linkwithInfo); $queryData = explode('?', $linkParts[0]); $relationData = explode('=', str_replace('"', '', $linkParts[1])); $relation = $relationData[1]; $links[$relation] = substr($queryData[1], 0, -1); } } } $result = [ 'data' => $response, 'links' => $links ]; if($returnFormated){ $result['data'] = json_decode($response,true); } return $result; } }