app = $app; $this->apiUrl = rtrim($apiUrl, '/') . '/'; $this->user = $user; $this->pass = $pass; } /** * @param $method * @param $endpoint * @param string $data * * @param string $storeview * @return array */ public function call($method,$endpoint,$data='',$storeview='') { if(empty($this->accessToken)){ $tokenResult = $this->checkConnection(); if(!$tokenResult['success']){ return $tokenResult; } } $url = $this->apiUrl; if (substr($url, -1) !== '/') { $url .= '/'; } if(!empty($storeview) && substr($storeview, -1) !== '/'){ $storeview .= '/'; } $ch = curl_init(); $setHeaders = array('Content-Type:application/json','Authorization:Bearer '.$this->accessToken); curl_setopt($ch, CURLOPT_URL, $url.'rest/'.$storeview.'V1/'.$endpoint); if(!empty($data)){ curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_HTTPHEADER, $setHeaders); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); $response = [ 'success' => false, 'message' => '', 'data' => null ]; $response['data'] = json_decode($result, true); if(curl_error($ch)) { $response['message']= curl_error($ch); }else{ $response['success'] = true; } curl_close($ch); return $response; } /** * @return array */ public function checkConnection() { if(!empty($this->accessToken)){ return [ 'success' => true, 'message' => $this->accessToken ]; } $data = [ 'username' => $this->user, 'password' => $this->pass ]; $data_string = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $this->apiUrl . 'rest/V1/integration/admin/token'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string) ] ); $resultJson = curl_exec($ch); $result = json_decode($resultJson,true); $response = [ 'success' => false, 'message' => 'Keine Rückmeldung von API.' ]; if(!empty($result['message'])){ $response['message'] = $result['message']; }elseif(!empty($result)){ $this->accessToken = $result; $response['message'] = $result; $response['success'] = true; curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type:application/json', 'Authorization:Bearer '.$this->accessToken ] ); } return $response; } /** * */ public function resetToken(){ $this->accessToken = null; } }